add unyaffs2tool.py 可以拆分file.bin为4个yaffs image
authorSnickerSun <snickersun@163.com>
Wed, 17 Jul 2024 09:50:27 +0000 (17:50 +0800)
committerSnickerSun <snickersun@163.com>
Wed, 17 Jul 2024 09:50:27 +0000 (17:50 +0800)
code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/unyaffs2tool.py [new file with mode: 0644]

diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/unyaffs2tool.py b/code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/unyaffs2tool.py
new file mode 100644 (file)
index 0000000..a7ba124
--- /dev/null
@@ -0,0 +1,167 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import time
+import struct
+import shutil
+
+"""
+介绍:
+    本脚本主要实现调用mkyaffs2image工具生成文件镜像
+    请注意使用本脚本时支持的python版本,本脚本只支持python3版本。
+    请根据实际情况配置对应信息,主要是nandflash的信息,以及分区信息
+
+"""
+
+root_path = os.path.join(os.path.split(os.path.realpath(__file__))[0], '..', '..', '..', '..', '..', '..')
+cur_dir = os.path.dirname(os.path.abspath(__file__))
+image_tool_path = os.path.realpath(os.path.join(root_path, 'tools', 'bin', 'platform_tool', 'unyaffs'))
+
+# 配置生成文件镜像工具路径
+mkyaffstool = os.path.join(image_tool_path, 'unyaffs')
+image_name = "file"
+
+# 配置nandflash信息
+cfg_page_size = 2048
+cfg_obb_size = 64
+
+
+#code/sdk/tools/bin/platform_tool/mkyaffs2image/mkyaffs2image code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/fs/system code/sdk/build/config/target_config/brandy/mk_fs_image/fd230/file_tmp.bin 1 2 0xcc0000
+#code/sdk/tools/bin/platform_tool/mkyaffs2image/mkyaffs2image code/sdk/build/config/target_config/brandy/mk_fs_image/fs/update code/sdk/build/config/target_config/brandy/mk_fs_image/file_tmp.bin 1 2 0x1980000
+#code/sdk/tools/bin/platform_tool/mkyaffs2image/mkyaffs2image code/sdk/build/config/target_config/brandy/mk_fs_image/fs/music code/sdk/build/config/target_config/brandy/mk_fs_image/file_tmp.bin 1 2 0x2640000
+#code/sdk/tools/bin/platform_tool/mkyaffs2image/mkyaffs2image code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user code/sdk/build/config/target_config/brandy/mk_fs_image/file_tmp.bin 1 2
+# pagesize 2048 oobsize 64
+# 配置分区信息,请根据实际情况进行配置,添加格式为[分区路径, 分区大小]
+# 分区大小需要根据按照百分系统分配好后的blocks数进行计算,大小=blocks(文件块个数)*pages_per_block(每个文件块包含的页数)*bytes_per_page(每页包含字节数)
+# 前面按照比例分好分区,最后分区置成null,即代表剩下所有blocks
+# drivers/drivers/driver/nand_flash/nand/nandflash_config.c 
+# #ifndef MEMORY_MINI
+# #define YF_NAME_STRS "/system/", "/update/", "/music/", "/user/"
+# uint32_t yf_volume_percent[YF_VOLUMES_NUM] = { 1, 1, 1, 97 };
+#                                               0x140000
+#                                                10 10 10 70
+#                                               0xcc0000
+
+
+partition_list = [
+    ["system","0x0", "0xcc0000"],
+    ["update","0xcc0000","0xcc0000"],
+    ["music", "0x1980000","0xcc0000"],
+    ["user", "0x2640000","null"]
+]
+
+# 将生成image文件转换大小端,保存成另外文件
+def convert(src_file, dest_file):
+    fp_src = open(src_file, 'rb')
+    fp_dest = open(dest_file, 'wb')
+    src_size = os.path.getsize(src_file)
+    for i in range(int(src_size / 4)):
+        data = fp_src.read(4)
+        rw = struct.pack('BBBB', data[3], data[2], data[1], data[0])
+        fp_dest.write(rw)
+    fp_src.close()
+    fp_dest.close()
+
+def splitimg(src_file,dest_file,startaddr,imgsize):
+    fp_src = open(src_file, 'rb')
+    fp_dest = open(dest_file, 'wb')
+    src_size = os.path.getsize(src_file)
+    print("{} {} {} {}".format(src_file,dest_file,startaddr,imgsize))
+    for i in range(int(src_size / 4)):
+        data = fp_src.read(4)
+        if  i*4 >= startaddr and i*4 < (startaddr+imgsize):
+            # print(" write {} size {}".format(i*4,readsize))
+            rw = struct.pack('BBBB', data[3], data[2], data[1], data[0])
+            fp_dest.write(rw)            
+    fp_src.close()
+    fp_dest.close()
+def split_img(src_file):
+    fileindex = 0
+    fp_src = open(src_file, 'rb')
+    dest_file = os.path.join(cur_dir, "%d_tmp.bin" %fileindex)
+    src_size = os.path.getsize(src_file)
+    fp_dest = open(dest_file, 'wb')
+    yaffsheader = struct.pack('BBBB', 0x59,0x46, 0x53, 0x53)
+    print("read {} ".format(yaffsheader))
+    for i in range(int(src_size / 4)):
+        data = fp_src.read(4)
+        rw = struct.pack('BBBB', data[3], data[2], data[1], data[0])
+        if data[0]==0x53 and data[1]==0x53 and data[2]==0x46 and data[3]==0x59:
+        # if data[0]==0x59 and data[1]==0x46 and data[2]==0x53 and data[3]==0x53:
+            if fileindex >= 0 :
+                fp_dest.close()
+                print("write {} size {}".format(dest_file,os.path.getsize(dest_file)))
+                dest_file = os.path.join(cur_dir, "%d_tmp.bin" %fileindex)
+                fp_dest = open(dest_file, 'wb')
+            fileindex+=1
+        fp_dest.write(rw)            
+    fp_src.close()
+    fp_dest.close()
+
+
+def get_page_type(page_size):
+    if page_size == 2048:
+        return " 1"
+    elif page_size == 4096:
+        return " 2"
+    else:
+        return " 0"
+
+def get_obb_type(obb_size):
+    if obb_size == 64:
+        return " 2"
+    elif obb_size == 128:
+        return " 4"
+    else:
+        return " 0"
+
+if __name__ == "__main__":
+    image_file = os.path.join(cur_dir, "file.bin")
+
+    if sys.platform == 'linux2' or sys.platform == 'linux':
+        partition_total = 0
+        # page_type =  get_page_type(cfg_page_size)
+        # obb_type = get_obb_type(cfg_obb_size)
+        page_type = " 1"
+        obb_type = " 2"
+
+        # if os.path.exists(tmp_file):
+        #     os.remove(tmp_file)
+        # if os.path.exists(image_file):
+        #     os.remove(image_file)
+        imgfilesize = os.path.getsize(image_file)
+
+        split_img(image_file)
+        # for partition in partition_list:
+        #     tmp_file = os.path.join(cur_dir, "%s_tmp.bin" %partition[0])
+        #     if (partition[2] != "null"):
+        #         partition_total = partition_total + int(partition[2][2:], 16)
+        #         splitimg(image_file,tmp_file,int(partition[1][2:], 16),int(partition[2][2:], 16))
+        #     else:
+        #         splitimg(image_file,tmp_file,int(partition[1][2:], 16), (imgfilesize - partition_total ))            
+                
+            
+            # os.system(command)
+        # convert(tmp_file, image_file)
+        #unyaffs V0.9.7 - extract files from a YAFFS2 file system image.
+
+        # Usage: unyaffs [options] <image_file_name> [<extract_directory>]
+
+        # Options:
+        #     -d               detection of flash layout, no extraction
+        #     -b               spare contains bad block information
+        #     -c <chunk size>  set chunk size in KByte (default: autodetect, max: 16)
+        #     -s <spare size>  set spare size in Byte  (default: autodetect, max: 512)
+        #     -t               list image contents
+        #     -v               verbose output
+        #     -V               print version
+        
+        # unyaffs -c 2 -s 64 -b -t system_tmp.bin
+
+    # if (len(sys.argv) == 2 and os.path.exists(image_file)):
+        # out_path = os.path.join(sys.argv[1], "%s.bin" %image_name)
+        # shutil.copyfile(image_file, out_path)
+
+    print("make yaffs image file over!")