Changeset 95b730c2 in mainline for tools/mkfat.py
- Timestamp:
- 2008-10-27T19:48:07Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- de4a1cf
- Parents:
- 7a5cdded
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/mkfat.py
r7a5cdded r95b730c2 49 49 50 50 size = 0 51 files = 051 files = 2 52 52 53 53 for name in os.listdir(root): … … 69 69 return len(os.listdir(root)) 70 70 71 def write_file(path, outf, cluster_size, data_start, fat ):71 def write_file(path, outf, cluster_size, data_start, fat, reserved_clusters): 72 72 "Store the contents of a file" 73 73 74 74 size = os.path.getsize(path) 75 75 prev = -1 76 first = -176 first = 0 77 77 78 78 inf = file(path, "r") 79 79 rd = 0; 80 80 while (rd < size): 81 data = inf.read(cluster_size);82 83 81 empty_cluster = fat.index(0) 84 85 82 fat[empty_cluster] = 0xffff 83 86 84 if (prev != -1): 87 85 fat[prev] = empty_cluster … … 91 89 prev = empty_cluster 92 90 93 outf.seek(data_start + empty_cluster * cluster_size) 91 data = inf.read(cluster_size); 92 outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size) 94 93 outf.write(data) 95 94 rd += len(data) 96 95 inf.close() 96 97 return first, size 98 99 def write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster): 100 "Store the contents of a directory" 101 102 length = len(directory) 103 size = length * dirent_size 104 prev = -1 105 first = 0 106 107 i = 0 108 rd = 0; 109 while (rd < size): 110 if (prev != -1): 111 empty_cluster = fat.index(0) 112 fat[empty_cluster] = 0xffff 113 fat[prev] = empty_cluster 114 else: 115 first = empty_cluster 116 117 prev = empty_cluster 118 119 data = '' 120 data_len = 0 121 while ((i < length) and (data_len < cluster_size)): 122 if (i == 0): 123 directory[i].cluster = empty_cluster 124 125 data += directory[i].pack() 126 data_len += dirent_size 127 i += 1 128 129 outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size) 130 outf.write(data) 131 rd += len(data) 97 132 98 133 return first, size … … 114 149 """ 115 150 151 DOT_DIR_ENTRY = """little: 152 uint8_t signature /* 0x2e signature */ 153 char name[7] /* empty */ 154 char ext[3] /* empty */ 155 uint8_t attr /* file attributes */ 156 padding[1] /* reserved for NT */ 157 uint8_t ctime_fine /* create time (fine resolution) */ 158 uint16_t ctime /* create time */ 159 uint16_t cdate /* create date */ 160 uint16_t adate /* access date */ 161 padding[2] /* EA-index */ 162 uint16_t mtime /* modification time */ 163 uint16_t mdate /* modification date */ 164 uint16_t cluster /* first cluster */ 165 uint32_t size /* file size */ 166 """ 167 168 DOTDOT_DIR_ENTRY = """little: 169 uint8_t signature[2] /* 0x2e signature */ 170 char name[6] /* empty */ 171 char ext[3] /* empty */ 172 uint8_t attr /* file attributes */ 173 padding[1] /* reserved for NT */ 174 uint8_t ctime_fine /* create time (fine resolution) */ 175 uint16_t ctime /* create time */ 176 uint16_t cdate /* create date */ 177 uint16_t adate /* access date */ 178 padding[2] /* EA-index */ 179 uint16_t mtime /* modification time */ 180 uint16_t mdate /* modification date */ 181 uint16_t cluster /* first cluster */ 182 uint32_t size /* file size */ 183 """ 184 116 185 def mangle_fname(name): 117 186 # FIXME: filter illegal characters 118 fname = (name.split('.')[0] + ' ').upper()[0:8] 119 return fname 187 parts = name.split('.') 188 189 if (len(parts) > 0): 190 fname = parts[0] 191 else: 192 fname = '' 193 194 return (fname + ' ').upper()[0:8] 120 195 121 196 def mangle_ext(name): 122 197 # FIXME: filter illegal characters 123 ext = (name.split('.')[1] + ' ').upper()[0:3] 124 return ext 198 parts = name.split('.') 199 200 if (len(parts) > 1): 201 ext = parts[1] 202 else: 203 ext = '' 204 205 return (ext + ' ').upper()[0:3] 125 206 126 207 def create_dirent(name, directory, cluster, size): … … 142 223 dir_entry.mdate = 0 # FIXME 143 224 dir_entry.cluster = cluster 144 dir_entry.size = size 225 226 if (directory): 227 dir_entry.size = 0 228 else: 229 dir_entry.size = size 145 230 146 231 return dir_entry 147 232 148 def recursion(head, root, outf, cluster_size, root_start, data_start, fat): 233 def create_dot_dirent(empty_cluster): 234 dir_entry = xstruct.create(DOT_DIR_ENTRY) 235 236 dir_entry.signature = 0x2e 237 dir_entry.name = ' ' 238 dir_entry.ext = ' ' 239 dir_entry.attr = 0x10 240 241 dir_entry.ctime_fine = 0 # FIXME 242 dir_entry.ctime = 0 # FIXME 243 dir_entry.cdate = 0 # FIXME 244 dir_entry.adate = 0 # FIXME 245 dir_entry.mtime = 0 # FIXME 246 dir_entry.mdate = 0 # FIXME 247 dir_entry.cluster = empty_cluster 248 dir_entry.size = 0 249 250 return dir_entry 251 252 def create_dotdot_dirent(parent_cluster): 253 dir_entry = xstruct.create(DOTDOT_DIR_ENTRY) 254 255 dir_entry.signature = [0x2e, 0x2e] 256 dir_entry.name = ' ' 257 dir_entry.ext = ' ' 258 dir_entry.attr = 0x10 259 260 dir_entry.ctime_fine = 0 # FIXME 261 dir_entry.ctime = 0 # FIXME 262 dir_entry.cdate = 0 # FIXME 263 dir_entry.adate = 0 # FIXME 264 dir_entry.mtime = 0 # FIXME 265 dir_entry.mdate = 0 # FIXME 266 dir_entry.cluster = parent_cluster 267 dir_entry.size = 0 268 269 return dir_entry 270 271 def recursion(head, root, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, parent_cluster): 149 272 "Recursive directory walk" 150 273 151 274 directory = [] 275 276 if (not head): 277 # Directory cluster preallocation 278 empty_cluster = fat.index(0) 279 fat[empty_cluster] = 0xffff 280 281 directory.append(create_dot_dirent(empty_cluster)) 282 directory.append(create_dotdot_dirent(parent_cluster)) 283 else: 284 empty_cluster = 0 285 152 286 for name in os.listdir(root): 153 287 canon = os.path.join(root, name) 154 288 155 289 if (os.path.isfile(canon)): 156 rv = write_file(canon, outf, cluster_size, data_start, fat )290 rv = write_file(canon, outf, cluster_size, data_start, fat, reserved_clusters) 157 291 directory.append(create_dirent(name, False, rv[0], rv[1])) 292 293 if (os.path.isdir(canon)): 294 rv = recursion(False, canon, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, empty_cluster) 295 directory.append(create_dirent(name, True, rv[0], rv[1])) 158 296 159 297 if (head): … … 161 299 for dir_entry in directory: 162 300 outf.write(dir_entry.pack()) 301 else: 302 return write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster) 163 303 164 304 BOOT_SECTOR = """little: … … 212 352 213 353 fat16_clusters = 4096 354 min_cluster_size = 1024 214 355 215 356 sector_size = 512 … … 223 364 size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size 224 365 while (size / cluster_size < fat16_clusters): 225 if (cluster_size > sector_size):366 if (cluster_size > min_cluster_size): 226 367 cluster_size /= 2 227 368 size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size … … 272 413 # FAT tables 273 414 for i in range(0, fat_count): 274 for j in range(0, boot_sector.fat_sectors):415 for j in range(0, fat_size / sector_size): 275 416 outf.write(empty_sector.pack()) 276 417 … … 287 428 fat[1] = 0xffff 288 429 289 recursion(True, path, outf, cluster_size, root_start, data_start, fat )430 recursion(True, path, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, 0) 290 431 291 432 # Store FAT
Note:
See TracChangeset
for help on using the changeset viewer.