From 6328b439574b98a46e411e8bacc69e6e0af94042 Mon Sep 17 00:00:00 2001 From: nganhkhoa Date: Sun, 18 Aug 2024 03:49:34 +0700 Subject: [PATCH] basic cli parsing --- .gitignore | 30 ++++++++++++++++++++++++++++++ bin/detect.ml | 10 ++++++++++ bin/dune | 6 ++++++ bin/extract.ml | 4 ++++ bin/main.ml | 32 ++++++++++++++++++++++++++++++++ dune-project | 26 ++++++++++++++++++++++++++ firmex.opam | 0 lib/archive/zip.ml | 14 ++++++++++++++ lib/dune | 3 +++ test/dune | 2 ++ test/test_firmex.ml | 0 11 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 bin/detect.ml create mode 100644 bin/dune create mode 100644 bin/extract.ml create mode 100644 bin/main.ml create mode 100644 dune-project create mode 100644 firmex.opam create mode 100644 lib/archive/zip.ml create mode 100644 lib/dune create mode 100644 test/dune create mode 100644 test/test_firmex.ml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6e44f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +### OCaml ### +*.annot +*.cmo +*.cma +*.cmi +*.a +*.o +*.cmx +*.cmxs +*.cmxa + +# ocamlbuild working directory +_build/ + +# ocamlbuild targets +*.byte +*.native + +# oasis generated files +setup.data +setup.log + +# Merlin configuring file for Vim and Emacs +.merlin + +# Dune generated files +*.install + +# Local OPAM switch +_opam/ diff --git a/bin/detect.ml b/bin/detect.ml new file mode 100644 index 0000000..dfa8dc8 --- /dev/null +++ b/bin/detect.ml @@ -0,0 +1,10 @@ +(** detection module + + detect a given file for formats or ISA + *) + +open Printf + +let runner isa file = + printf "Detect file isa=%b %s\n" isa file + diff --git a/bin/dune b/bin/dune new file mode 100644 index 0000000..2e9e5dd --- /dev/null +++ b/bin/dune @@ -0,0 +1,6 @@ +(env (dev (flags :standard -warn-error -27-32))) + +(executable + (public_name firmex) + (name main) + (libraries firmex cmdliner)) diff --git a/bin/extract.ml b/bin/extract.ml new file mode 100644 index 0000000..7f7429b --- /dev/null +++ b/bin/extract.ml @@ -0,0 +1,4 @@ +open Printf + +let runner dry file = + printf "Extract file dry=%b %s\n" dry file diff --git a/bin/main.ml b/bin/main.ml new file mode 100644 index 0000000..e6589c2 --- /dev/null +++ b/bin/main.ml @@ -0,0 +1,32 @@ +(** Firmware extractor + + 1. Verify if the file is a welknown archive format + 2. Checking internal bytes for common filesystem + 3. Recursive + + Some files might require different approach + + Common archive file formats are ZIP XZ TAR + Common filesystem are SQUASHFS JIFFS + *) + +open Cmdliner + +let extract = + let info = Cmd.info "extract" ~doc:"Extract the file." in + let dry = Arg.(value & opt bool false & info ["d"; "dry"] ~docv:"false" ~doc:"Dry run") in + let file = Arg.(required & pos 0 (some string) None & info [] ~docv:"FILE") in + Cmd.v info Term.(const Extract.runner $ dry $ file) + +let detect = + let info = Cmd.info "detect" ~doc:"Detect the file type and its internal contents." in + let isa = Arg.(value & opt bool false & info ["isa"] ~docv:"false" ~doc:"Detect Instruction Set Architecture") in + let file = Arg.(required & pos 0 (some string) None & info [] ~docv:"FILE") in + Cmd.v info Term.(const Detect.runner $ isa $ file) + +let cmd = + let info = Cmd.info "firmex group" ~version:"0.0.0-alpha" in + Cmd.group info [extract; detect;] + + +let () = exit (Cmd.eval cmd) diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..6dd0d80 --- /dev/null +++ b/dune-project @@ -0,0 +1,26 @@ +(lang dune 3.16) + +(name firmex) + +(generate_opam_files true) + +(source + (github username/reponame)) + +(authors "Author Name") + +(maintainers "Maintainer Name") + +(license LICENSE) + +(documentation https://url/to/documentation) + +(package + (name firmex) + (synopsis "A short synopsis") + (description "A longer description") + (depends ocaml dune cmdliner) + (tags + (topics "to describe" your project))) + +; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html diff --git a/firmex.opam b/firmex.opam new file mode 100644 index 0000000..e69de29 diff --git a/lib/archive/zip.ml b/lib/archive/zip.ml new file mode 100644 index 0000000..ed9a469 --- /dev/null +++ b/lib/archive/zip.ml @@ -0,0 +1,14 @@ +(** This modules handle zip archive format + + ZIP file format is actually really easy to parse + the file is a list of files compressed + each entry is appended with a header + + ZIP file supports multiple compression algorithm, + the most frequently used is LZMA + files can be encrypted using a weak algorithm + which has been deprecated or AES + *) + +let signature = Bytes.of_string "PK\x00\x00" + diff --git a/lib/dune b/lib/dune new file mode 100644 index 0000000..8c98bbd --- /dev/null +++ b/lib/dune @@ -0,0 +1,3 @@ +(include_subdirs qualified) +(library + (name firmex)) diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..ea57084 --- /dev/null +++ b/test/dune @@ -0,0 +1,2 @@ +(test + (name test_firmex)) diff --git a/test/test_firmex.ml b/test/test_firmex.ml new file mode 100644 index 0000000..e69de29