Skip to content

Commit

Permalink
add binary_len field to fs header and crc32 footer
Browse files Browse the repository at this point in the history
  • Loading branch information
jkent committed Feb 23, 2021
1 parent 8fd7319 commit 52b17b0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/espfs_format.h → include/libespfs/espfs_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
*/
#define ESPFS_MAGIC 0x2B534645 /** EFS+ */
#define ESPFS_VERSION_MAJOR 0
#define ESPFS_VERSION_MINOR 0
#define ESPFS_VERSION_MINOR 1

typedef struct espfs_fs_header_t espfs_fs_header_t;
typedef struct espfs_hashtable_entry_t espfs_hashtable_entry_t;
typedef struct espfs_object_header_t espfs_object_header_t;
typedef struct espfs_dir_header_t espfs_dir_header_t;
typedef struct espfs_file_header_t espfs_file_header_t;
typedef struct espfs_heatshrink_header_t espfs_heatshrink_header_t;
typedef struct espfs_crc32_footer_t espfs_crc32_footer_t;

struct espfs_fs_header_t {
uint32_t magic;
uint8_t len;
uint8_t version_major;
uint16_t version_minor;
uint32_t num_objects;
uint32_t binary_len;
} __attribute__((packed));

struct espfs_hashtable_entry_t {
Expand Down Expand Up @@ -58,3 +60,7 @@ struct espfs_heatshrink_header_t {
uint8_t lookahead_sz2;
uint16_t reserved;
} __attribute__((packed));

struct espfs_crc32_footer_t {
uint32_t crc32;
} __attribute__((packed));
1 change: 1 addition & 0 deletions src/espfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "espfs_priv.h"
#include "log.h"
#include "libespfs/espfs.h"
#include "libespfs/espfs_format.h"

#if defined(CONFIG_ESPFS_USE_HEATSHRINK)
# include "heatshrink_decoder.h"
Expand Down
2 changes: 1 addition & 1 deletion src/espfs_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "espfs_format.h"
#include "libespfs/espfs_format.h"

#if defined(ESP_PLATFORM)
# include <esp_spi_flash.h>
Expand Down
36 changes: 21 additions & 15 deletions tools/mkespfsimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,38 @@
from collections import OrderedDict
from fnmatch import fnmatch
from struct import Struct
from zlib import crc32

import heatshrink2
from hiyapyco import odyldo

script_dir = os.path.dirname(os.path.realpath(__file__))

# magic, len, version_major, version_minor, num_objects
espfs_fs_header_t = Struct('<IBBHI')
espfs_fs_header_t = Struct('<IBBHII')
# magic, len, version_major, version_minor, num_objects, file_len
ESPFS_MAGIC = 0x2B534645 # EFS+
ESPFS_VERSION_MAJOR = 0
ESPFS_VERSION_MINOR = 0
ESPFS_VERSION_MINOR = 1

# hash, offset
espfs_hashtable_t = Struct('<II')
# hash, offset

# type, len, path_len
espfs_object_header_t = Struct('<BBH')
# type, len, path_len
ESPFS_TYPE_FILE = 0
ESPFS_TYPE_DIR = 1

# data_len, file_len, flags, compression, reserved
espfs_file_header_t = Struct('<IIHBB')
# data_len, file_len, flags, compression, reserved
ESPFS_FLAG_GZIP = (1 << 1)
ESPFS_COMPRESSION_NONE = 0
ESPFS_COMPRESSION_HEATSHRINK = 1

# window_sz2, lookahead_sz2
espfs_heatshrink_header_t = Struct('<BBH')
# window_sz2, lookahead_sz2

espfs_crc32_footer_t = Struct('<I')
# crc32

def load_config(root):
global config
Expand Down Expand Up @@ -96,10 +100,6 @@ def __lt__(self, other):
config['paths'] = OrderedDict(sorted(config['paths'].items(),
key = pattern_sort))

def make_fs_header(num_objects):
return espfs_fs_header_t.pack(ESPFS_MAGIC, espfs_fs_header_t.size,
ESPFS_VERSION_MAJOR, ESPFS_VERSION_MINOR, num_objects)

def hash_path(path):
hash = 5381
for c in path.encode('utf8'):
Expand Down Expand Up @@ -239,8 +239,7 @@ def main():
subprocess.check_call('npm install %s' % (npm), shell = True)

num_objects = len(pathlist)
fs_header = make_fs_header(num_objects)
offset = espfs_file_header_t.size + (espfs_hashtable_t.size * num_objects)
offset = espfs_fs_header_t.size + (espfs_hashtable_t.size * num_objects)
hashtable = b''
objects = b''

Expand All @@ -252,13 +251,20 @@ def main():
data = f.read()
object = make_file_object(hash, path, data, attributes['actions'])
else:
continue
print('unknown object type %d' % (type), file = sys.stderr)
sys.exit(1)
hashtable += espfs_hashtable_t.pack(hash, offset)
objects += object
offset += len(object)

binary_len = offset + espfs_crc32_footer_t.size
header = espfs_fs_header_t.pack(ESPFS_MAGIC, espfs_fs_header_t.size,
ESPFS_VERSION_MAJOR, ESPFS_VERSION_MINOR, num_objects, binary_len)
binary = header + hashtable + objects
binary += espfs_crc32_footer_t.pack(crc32(binary) & 0xFFFFFFFF)

with open(args.IMAGE, 'wb') as f:
f.write(fs_header + hashtable + objects)
f.write(binary)

if __name__ == '__main__':
main()

0 comments on commit 52b17b0

Please sign in to comment.