/ tools / spiffs / gen_spiffs_image.py
gen_spiffs_image.py
 1  
 2  from __future__ import print_function
 3  import argparse
 4  import os
 5  import re
 6  
 7  in_dir  = "fs"
 8  out_dir = "fs_image"
 9  out_name = "maixpy_spiffs.img"
10  
11  parser = argparse.ArgumentParser(description='generate spiffs image', prog="gen_spiffs_image.py")
12  parser.add_argument("config_file", help = "project config file path, MUST the same with firmware's", default="")
13  
14  args = parser.parse_args()
15  
16  if args.config_file == "" or not os.path.exists(args.config_file):
17      print("file {} not found".format(args.config_file))
18      exit(1)
19  
20  if not os.path.exists(in_dir):
21      print("file system dir not found:"+in_dir)
22      print("please put files in "+in_dir+" folder")
23      os.mkdir(in_dir)
24      exit(1)
25  
26  if len(os.listdir(in_dir)) == 0:
27      print("[WARNING] No file in fs dir, so the image will be empty")
28   
29  # check submodule
30  if len(os.listdir("mkspiffs")) == 0:
31      ret = os.system("git submodule update --init mkspiffs")
32      if ret != 0:
33          print("[ERROR] update submodule mkspiffs fail")
34          exit(1)
35  
36  if len(os.listdir("mkspiffs/spiffs")) == 0:
37      os.chdir("mkspiffs")
38      ret = os.system("git submodule update --init spiffs")
39      if ret != 0:
40          print("[ERROR] update submodule mkspiffs/spiffs fail")
41          exit(1)
42      os.chdir("..")
43  
44  f = open(args.config_file)
45  content = f.read()
46  f.close()
47  
48  match = re.findall(r"{}(.*){}(.*)\n".format("CONFIG_SPIFFS_", "="), content)
49  config = {}
50  for item in match:
51      v = item[1]
52      if v == "y":
53          v = "1"
54      config["SPIFFS_"+item[0]] = v
55  
56  
57  flags =  ""
58  for key in config.keys():
59      flags += ' -D'+key+'='+config[key]
60  
61  flags += ' -DSPIFFS_READ_ONLY=0 -DSPIFFS_ALIGNED_OBJECT_INDEX_TABLES=1 -DSPIFFS_SINGLETON=0 -DSPIFFS_HAL_CALLBACK_EXTRA=0'
62  
63  gen_exe_cmd = 'cd mkspiffs && make clean && make dist CPPFLAGS="'+flags+'" BUILD_CONFIG_NAME=-maixpy'
64  print("--------------------")
65  print(gen_exe_cmd)
66  print("--------------------")
67  ret = os.system(gen_exe_cmd)
68  if ret != 0:
69      exit(1)
70  
71  
72  
73  if not os.path.exists(out_dir):
74      os.mkdir(out_dir)
75  
76  dirs = os.listdir("mkspiffs")
77  exe_path = None
78  for dir in dirs:
79      if os.path.isfile(os.path.join("mkspiffs", dir)):
80          continue
81      match = re.findall(r"{}(.*){}(.*)".format("mkspiffs", "maixpy"), dir)
82      if len(match) > 0:
83          exe_path = os.path.abspath("mkspiffs/"+dir+"/mkspiffs")
84          break
85  print(exe_path)
86  logical_block_size = int(config["SPIFFS_LOGICAL_BLOCK_SIZE"], 16)
87  logical_page_size  = int(config["SPIFFS_LOGICAL_PAGE_SIZE"], 16)
88  image_size = int(config["SPIFFS_SIZE"], 16)
89  flash_addr = int(config["SPIFFS_START_ADDR"], 16)
90  gen_image_cmd = "{} -c {} -b {} -p {} -s {} {}/{}".format(exe_path, in_dir, logical_block_size, logical_page_size, image_size, out_dir, out_name)
91  print("--------------------")
92  print(gen_image_cmd)
93  print("--------------------")
94  ret = os.system(gen_image_cmd)
95  if ret != 0:
96      exit(1)
97  print("[SUCCESS] image file in "+out_dir)
98  print("[USAGE] Please flash {}/{} to address 0x{:X} of flash with flash tool kflash_gui".format(out_dir, out_name, flash_addr))
99