Skip to content

Commit

Permalink
suport offset for read and extract for partitioned disk iamges
Browse files Browse the repository at this point in the history
  • Loading branch information
Vysakh Pillai committed Feb 3, 2023
1 parent 94cfe75 commit f8308f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ required arguments:
Tool to list contents of a littleFS filesystem image as a tree.

```bash
usage: littlefs_list [-h] [-b BLOCKSIZE] [-c BLOCKCOUNT] [-v] -i IMAGE
usage: littlefs_list [-h] [-b BLOCKSIZE] [-c BLOCKCOUNT] [-o OFFSET] [-v] -i IMAGE

Tool to list files in a littlefs file image

Expand All @@ -56,6 +56,8 @@ optional arguments:
block size of the LFS image (defaults to 4096)
-c BLOCKCOUNT, --block_count BLOCKCOUNT
block Count of the LFS image (defaults to 64)
-o OFFSET, --offset OFFSET
offset (in bytes) from which the littlefs image starts(defaults to 0). Hex values are supported (e.g. 0x80000)
-v, --verbose Verbose

required arguments:
Expand All @@ -68,7 +70,7 @@ required arguments:
Tool to extract contents of a littleFS filesystem image to a destination folder.

```bash
usage: littlefs_extract [-h] [-b BLOCKSIZE] [-c BLOCKCOUNT] [-f] [-v] -i IMAGE -d DESTINATION
usage: littlefs_extract [-h] [-b BLOCKSIZE] [-c BLOCKCOUNT] [-f] [-o OFFSET] [-v] -i IMAGE -d DESTINATION

Tool to extract files from a littlefs file image

Expand All @@ -79,6 +81,8 @@ optional arguments:
-c BLOCKCOUNT, --block_count BLOCKCOUNT
block Count of the LFS image (defaults to 64)
-f, --force Force extract even if destination folder is not empty
-o OFFSET, --offset OFFSET
offset (in bytes) from which the littlefs image starts(defaults to 0). Hex values are supported (e.g. 0x80000)
-v, --verbose Verbose

required arguments:
Expand Down
25 changes: 25 additions & 0 deletions littlefs_tools/littlefs_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def sizeof_fmt(num, suffix="B"):
num /= 1024.0
return f"{num:.1f}Yi{suffix}"

def _offset_to_int(offset):
if offset.startswith("0x"):
return int(offset, 16)
else:
return int(offset)

def _print_tree(fs, path):
try:
Expand All @@ -57,6 +62,7 @@ def _lsfiles(args):
block_size=args.blockSize, block_count=args.blockCount, mount=False
)
with open(args.image, "rb") as fh:
fh.seek(args.offset)
fs.context.buffer = bytearray(fh.read())
fs.mount()
print(f"{Fore.GREEN}{args.image}")
Expand Down Expand Up @@ -87,6 +93,14 @@ def list_files():
type=int,
default=64,
)
parser.add_argument(
"-o",
"--offset",
dest="offset",
help="offset (in bytes) from which the littlefs image starts(defaults to 0). Hex values are supported (e.g. 0x80000)",
type=str,
default="0",
)
parser.add_argument("-v", "--verbose", help="Verbose", action="store_true")

requiredNamed = parser.add_argument_group("required arguments")
Expand All @@ -96,6 +110,7 @@ def list_files():
args = parser.parse_args()

set_log_level(args.verbose)
args.offset = _offset_to_int(args.offset)

_lsfiles(args)

Expand Down Expand Up @@ -137,6 +152,7 @@ def _extract_files(args):
block_size=args.blockSize, block_count=args.blockCount, mount=False
)
with open(args.image, "rb") as fh:
fh.seek(args.offset)
fs.context.buffer = bytearray(fh.read())
fs.mount()
Path(args.destination).mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -174,6 +190,14 @@ def extract_files():
help="Force extract even if destination folder is not empty",
action="store_true",
)
parser.add_argument(
"-o",
"--offset",
dest="offset",
help="offset (in bytes) from which the littlefs image starts(defaults to 0). Hex values are supported (e.g. 0x80000)",
type=str,
default="0",
)
parser.add_argument("-v", "--verbose", help="Verbose", action="store_true")

requiredNamed = parser.add_argument_group("required arguments")
Expand All @@ -190,6 +214,7 @@ def extract_files():
args = parser.parse_args()

set_log_level(args.verbose)
args.offset = _offset_to_int(args.offset)

_extract_files(args)

Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="littlefs_tools",
version="1.0.5",
version="1.1.0",
description="Python package to create littleFS filesystem image binaries",
long_description=long_description,
long_description_content_type="text/markdown",
Expand All @@ -31,12 +31,10 @@
"Topic :: System :: Filesystems",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10"
"Programming Language :: Python :: 3.11"
],
)

0 comments on commit f8308f8

Please sign in to comment.