[5749372] | 1 | #!/usr/bin/env python
|
---|
| 2 | #
|
---|
| 3 | # Copyright (c) 2008 Martin Decky
|
---|
| 4 | # All rights reserved.
|
---|
| 5 | #
|
---|
| 6 | # Redistribution and use in source and binary forms, with or without
|
---|
| 7 | # modification, are permitted provided that the following conditions
|
---|
| 8 | # are met:
|
---|
| 9 | #
|
---|
| 10 | # - Redistributions of source code must retain the above copyright
|
---|
| 11 | # notice, this list of conditions and the following disclaimer.
|
---|
| 12 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | # documentation and/or other materials provided with the distribution.
|
---|
| 15 | # - The name of the author may not be used to endorse or promote products
|
---|
| 16 | # derived from this software without specific prior written permission.
|
---|
| 17 | #
|
---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | #
|
---|
[3c80f2b] | 29 |
|
---|
[5749372] | 30 | """
|
---|
| 31 | FAT creator
|
---|
| 32 | """
|
---|
| 33 |
|
---|
| 34 | import sys
|
---|
| 35 | import os
|
---|
| 36 | import random
|
---|
| 37 | import xstruct
|
---|
[04619ba] | 38 | import array
|
---|
[5749372] | 39 |
|
---|
[24edc18] | 40 | exclude_names = set(['.svn', '.bzr'])
|
---|
[9df7918] | 41 |
|
---|
[700eaa9] | 42 | def align_up(size, alignment):
|
---|
| 43 | "Return size aligned up to alignment"
|
---|
| 44 |
|
---|
| 45 | if (size % alignment == 0):
|
---|
| 46 | return size
|
---|
| 47 |
|
---|
[28f4adb] | 48 | return ((size // alignment) + 1) * alignment
|
---|
[700eaa9] | 49 |
|
---|
[04619ba] | 50 | def subtree_size(root, cluster_size, dirent_size):
|
---|
[700eaa9] | 51 | "Recursive directory walk and calculate size"
|
---|
| 52 |
|
---|
| 53 | size = 0
|
---|
[95b730c2] | 54 | files = 2
|
---|
[700eaa9] | 55 |
|
---|
| 56 | for name in os.listdir(root):
|
---|
| 57 | canon = os.path.join(root, name)
|
---|
| 58 |
|
---|
[ab579fa] | 59 | if (os.path.isfile(canon) and (not name in exclude_names)):
|
---|
[700eaa9] | 60 | size += align_up(os.path.getsize(canon), cluster_size)
|
---|
| 61 | files += 1
|
---|
| 62 |
|
---|
[ab579fa] | 63 | if (os.path.isdir(canon) and (not name in exclude_names)):
|
---|
[04619ba] | 64 | size += subtree_size(canon, cluster_size, dirent_size)
|
---|
[700eaa9] | 65 | files += 1
|
---|
| 66 |
|
---|
[04619ba] | 67 | return size + align_up(files * dirent_size, cluster_size)
|
---|
[700eaa9] | 68 |
|
---|
| 69 | def root_entries(root):
|
---|
| 70 | "Return number of root directory entries"
|
---|
| 71 |
|
---|
| 72 | return len(os.listdir(root))
|
---|
| 73 |
|
---|
[95b730c2] | 74 | def write_file(path, outf, cluster_size, data_start, fat, reserved_clusters):
|
---|
[04619ba] | 75 | "Store the contents of a file"
|
---|
| 76 |
|
---|
| 77 | size = os.path.getsize(path)
|
---|
| 78 | prev = -1
|
---|
[95b730c2] | 79 | first = 0
|
---|
[04619ba] | 80 |
|
---|
[28f4adb] | 81 | inf = open(path, "rb")
|
---|
[04619ba] | 82 | rd = 0;
|
---|
| 83 | while (rd < size):
|
---|
| 84 | empty_cluster = fat.index(0)
|
---|
| 85 | fat[empty_cluster] = 0xffff
|
---|
[95b730c2] | 86 |
|
---|
[04619ba] | 87 | if (prev != -1):
|
---|
| 88 | fat[prev] = empty_cluster
|
---|
| 89 | else:
|
---|
| 90 | first = empty_cluster
|
---|
| 91 |
|
---|
| 92 | prev = empty_cluster
|
---|
| 93 |
|
---|
[28f4adb] | 94 | data = bytes(inf.read(cluster_size));
|
---|
[95b730c2] | 95 | outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size)
|
---|
[04619ba] | 96 | outf.write(data)
|
---|
| 97 | rd += len(data)
|
---|
| 98 | inf.close()
|
---|
| 99 |
|
---|
| 100 | return first, size
|
---|
| 101 |
|
---|
[95b730c2] | 102 | def write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster):
|
---|
| 103 | "Store the contents of a directory"
|
---|
| 104 |
|
---|
| 105 | length = len(directory)
|
---|
| 106 | size = length * dirent_size
|
---|
| 107 | prev = -1
|
---|
| 108 | first = 0
|
---|
| 109 |
|
---|
| 110 | i = 0
|
---|
| 111 | rd = 0;
|
---|
| 112 | while (rd < size):
|
---|
| 113 | if (prev != -1):
|
---|
| 114 | empty_cluster = fat.index(0)
|
---|
| 115 | fat[empty_cluster] = 0xffff
|
---|
| 116 | fat[prev] = empty_cluster
|
---|
| 117 | else:
|
---|
| 118 | first = empty_cluster
|
---|
| 119 |
|
---|
| 120 | prev = empty_cluster
|
---|
| 121 |
|
---|
[28f4adb] | 122 | data = bytes()
|
---|
[95b730c2] | 123 | data_len = 0
|
---|
| 124 | while ((i < length) and (data_len < cluster_size)):
|
---|
| 125 | if (i == 0):
|
---|
| 126 | directory[i].cluster = empty_cluster
|
---|
| 127 |
|
---|
| 128 | data += directory[i].pack()
|
---|
| 129 | data_len += dirent_size
|
---|
| 130 | i += 1
|
---|
| 131 |
|
---|
| 132 | outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size)
|
---|
| 133 | outf.write(data)
|
---|
| 134 | rd += len(data)
|
---|
| 135 |
|
---|
| 136 | return first, size
|
---|
| 137 |
|
---|
[04619ba] | 138 | DIR_ENTRY = """little:
|
---|
| 139 | char name[8] /* file name */
|
---|
| 140 | char ext[3] /* file extension */
|
---|
| 141 | uint8_t attr /* file attributes */
|
---|
[a248234] | 142 | uint8_t lcase /* file name case (NT extension) */
|
---|
[04619ba] | 143 | uint8_t ctime_fine /* create time (fine resolution) */
|
---|
| 144 | uint16_t ctime /* create time */
|
---|
| 145 | uint16_t cdate /* create date */
|
---|
| 146 | uint16_t adate /* access date */
|
---|
| 147 | padding[2] /* EA-index */
|
---|
| 148 | uint16_t mtime /* modification time */
|
---|
| 149 | uint16_t mdate /* modification date */
|
---|
| 150 | uint16_t cluster /* first cluster */
|
---|
| 151 | uint32_t size /* file size */
|
---|
| 152 | """
|
---|
| 153 |
|
---|
[95b730c2] | 154 | DOT_DIR_ENTRY = """little:
|
---|
| 155 | uint8_t signature /* 0x2e signature */
|
---|
| 156 | char name[7] /* empty */
|
---|
| 157 | char ext[3] /* empty */
|
---|
| 158 | uint8_t attr /* file attributes */
|
---|
| 159 | padding[1] /* reserved for NT */
|
---|
| 160 | uint8_t ctime_fine /* create time (fine resolution) */
|
---|
| 161 | uint16_t ctime /* create time */
|
---|
| 162 | uint16_t cdate /* create date */
|
---|
| 163 | uint16_t adate /* access date */
|
---|
| 164 | padding[2] /* EA-index */
|
---|
| 165 | uint16_t mtime /* modification time */
|
---|
| 166 | uint16_t mdate /* modification date */
|
---|
| 167 | uint16_t cluster /* first cluster */
|
---|
| 168 | uint32_t size /* file size */
|
---|
| 169 | """
|
---|
| 170 |
|
---|
| 171 | DOTDOT_DIR_ENTRY = """little:
|
---|
| 172 | uint8_t signature[2] /* 0x2e signature */
|
---|
| 173 | char name[6] /* empty */
|
---|
| 174 | char ext[3] /* empty */
|
---|
| 175 | uint8_t attr /* file attributes */
|
---|
| 176 | padding[1] /* reserved for NT */
|
---|
| 177 | uint8_t ctime_fine /* create time (fine resolution) */
|
---|
| 178 | uint16_t ctime /* create time */
|
---|
| 179 | uint16_t cdate /* create date */
|
---|
| 180 | uint16_t adate /* access date */
|
---|
| 181 | padding[2] /* EA-index */
|
---|
| 182 | uint16_t mtime /* modification time */
|
---|
| 183 | uint16_t mdate /* modification date */
|
---|
| 184 | uint16_t cluster /* first cluster */
|
---|
| 185 | uint32_t size /* file size */
|
---|
| 186 | """
|
---|
| 187 |
|
---|
[04619ba] | 188 | def mangle_fname(name):
|
---|
| 189 | # FIXME: filter illegal characters
|
---|
[95b730c2] | 190 | parts = name.split('.')
|
---|
| 191 |
|
---|
| 192 | if (len(parts) > 0):
|
---|
| 193 | fname = parts[0]
|
---|
| 194 | else:
|
---|
| 195 | fname = ''
|
---|
| 196 |
|
---|
| 197 | return (fname + ' ').upper()[0:8]
|
---|
[04619ba] | 198 |
|
---|
| 199 | def mangle_ext(name):
|
---|
| 200 | # FIXME: filter illegal characters
|
---|
[95b730c2] | 201 | parts = name.split('.')
|
---|
| 202 |
|
---|
| 203 | if (len(parts) > 1):
|
---|
| 204 | ext = parts[1]
|
---|
| 205 | else:
|
---|
| 206 | ext = ''
|
---|
| 207 |
|
---|
| 208 | return (ext + ' ').upper()[0:3]
|
---|
[04619ba] | 209 |
|
---|
| 210 | def create_dirent(name, directory, cluster, size):
|
---|
| 211 | dir_entry = xstruct.create(DIR_ENTRY)
|
---|
| 212 |
|
---|
| 213 | dir_entry.name = mangle_fname(name)
|
---|
| 214 | dir_entry.ext = mangle_ext(name)
|
---|
| 215 |
|
---|
| 216 | if (directory):
|
---|
| 217 | dir_entry.attr = 0x30
|
---|
| 218 | else:
|
---|
| 219 | dir_entry.attr = 0x20
|
---|
| 220 |
|
---|
[a248234] | 221 | dir_entry.lcase = 0x18
|
---|
[04619ba] | 222 | dir_entry.ctime_fine = 0 # FIXME
|
---|
| 223 | dir_entry.ctime = 0 # FIXME
|
---|
| 224 | dir_entry.cdate = 0 # FIXME
|
---|
| 225 | dir_entry.adate = 0 # FIXME
|
---|
| 226 | dir_entry.mtime = 0 # FIXME
|
---|
| 227 | dir_entry.mdate = 0 # FIXME
|
---|
| 228 | dir_entry.cluster = cluster
|
---|
[95b730c2] | 229 |
|
---|
| 230 | if (directory):
|
---|
| 231 | dir_entry.size = 0
|
---|
| 232 | else:
|
---|
| 233 | dir_entry.size = size
|
---|
| 234 |
|
---|
| 235 | return dir_entry
|
---|
| 236 |
|
---|
| 237 | def create_dot_dirent(empty_cluster):
|
---|
| 238 | dir_entry = xstruct.create(DOT_DIR_ENTRY)
|
---|
| 239 |
|
---|
| 240 | dir_entry.signature = 0x2e
|
---|
| 241 | dir_entry.name = ' '
|
---|
| 242 | dir_entry.ext = ' '
|
---|
| 243 | dir_entry.attr = 0x10
|
---|
| 244 |
|
---|
| 245 | dir_entry.ctime_fine = 0 # FIXME
|
---|
| 246 | dir_entry.ctime = 0 # FIXME
|
---|
| 247 | dir_entry.cdate = 0 # FIXME
|
---|
| 248 | dir_entry.adate = 0 # FIXME
|
---|
| 249 | dir_entry.mtime = 0 # FIXME
|
---|
| 250 | dir_entry.mdate = 0 # FIXME
|
---|
| 251 | dir_entry.cluster = empty_cluster
|
---|
| 252 | dir_entry.size = 0
|
---|
| 253 |
|
---|
| 254 | return dir_entry
|
---|
| 255 |
|
---|
| 256 | def create_dotdot_dirent(parent_cluster):
|
---|
| 257 | dir_entry = xstruct.create(DOTDOT_DIR_ENTRY)
|
---|
| 258 |
|
---|
| 259 | dir_entry.signature = [0x2e, 0x2e]
|
---|
| 260 | dir_entry.name = ' '
|
---|
| 261 | dir_entry.ext = ' '
|
---|
| 262 | dir_entry.attr = 0x10
|
---|
| 263 |
|
---|
| 264 | dir_entry.ctime_fine = 0 # FIXME
|
---|
| 265 | dir_entry.ctime = 0 # FIXME
|
---|
| 266 | dir_entry.cdate = 0 # FIXME
|
---|
| 267 | dir_entry.adate = 0 # FIXME
|
---|
| 268 | dir_entry.mtime = 0 # FIXME
|
---|
| 269 | dir_entry.mdate = 0 # FIXME
|
---|
| 270 | dir_entry.cluster = parent_cluster
|
---|
| 271 | dir_entry.size = 0
|
---|
[04619ba] | 272 |
|
---|
| 273 | return dir_entry
|
---|
| 274 |
|
---|
[95b730c2] | 275 | def recursion(head, root, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, parent_cluster):
|
---|
[04619ba] | 276 | "Recursive directory walk"
|
---|
| 277 |
|
---|
| 278 | directory = []
|
---|
[95b730c2] | 279 |
|
---|
| 280 | if (not head):
|
---|
| 281 | # Directory cluster preallocation
|
---|
| 282 | empty_cluster = fat.index(0)
|
---|
| 283 | fat[empty_cluster] = 0xffff
|
---|
| 284 |
|
---|
| 285 | directory.append(create_dot_dirent(empty_cluster))
|
---|
| 286 | directory.append(create_dotdot_dirent(parent_cluster))
|
---|
| 287 | else:
|
---|
| 288 | empty_cluster = 0
|
---|
| 289 |
|
---|
[04619ba] | 290 | for name in os.listdir(root):
|
---|
| 291 | canon = os.path.join(root, name)
|
---|
| 292 |
|
---|
[9df7918] | 293 | if (os.path.isfile(canon) and (not name in exclude_names)):
|
---|
[95b730c2] | 294 | rv = write_file(canon, outf, cluster_size, data_start, fat, reserved_clusters)
|
---|
[04619ba] | 295 | directory.append(create_dirent(name, False, rv[0], rv[1]))
|
---|
[95b730c2] | 296 |
|
---|
[9df7918] | 297 | if (os.path.isdir(canon) and (not name in exclude_names)):
|
---|
[95b730c2] | 298 | rv = recursion(False, canon, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
|
---|
| 299 | directory.append(create_dirent(name, True, rv[0], rv[1]))
|
---|
[04619ba] | 300 |
|
---|
| 301 | if (head):
|
---|
| 302 | outf.seek(root_start)
|
---|
| 303 | for dir_entry in directory:
|
---|
| 304 | outf.write(dir_entry.pack())
|
---|
[95b730c2] | 305 | else:
|
---|
| 306 | return write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
|
---|
[04619ba] | 307 |
|
---|
[5749372] | 308 | BOOT_SECTOR = """little:
|
---|
| 309 | uint8_t jmp[3] /* jump instruction */
|
---|
| 310 | char oem[8] /* OEM string */
|
---|
| 311 | uint16_t sector /* bytes per sector */
|
---|
| 312 | uint8_t cluster /* sectors per cluster */
|
---|
| 313 | uint16_t reserved /* reserved sectors */
|
---|
| 314 | uint8_t fats /* number of FATs */
|
---|
| 315 | uint16_t rootdir /* root directory entries */
|
---|
| 316 | uint16_t sectors /* total number of sectors */
|
---|
| 317 | uint8_t descriptor /* media descriptor */
|
---|
| 318 | uint16_t fat_sectors /* sectors per single FAT */
|
---|
| 319 | uint16_t track_sectors /* sectors per track */
|
---|
| 320 | uint16_t heads /* number of heads */
|
---|
| 321 | uint32_t hidden /* hidden sectors */
|
---|
| 322 | uint32_t sectors_big /* total number of sectors (if sectors == 0) */
|
---|
| 323 |
|
---|
| 324 | /* Extended BIOS Parameter Block */
|
---|
| 325 | uint8_t drive /* physical drive number */
|
---|
| 326 | padding[1] /* reserved (current head) */
|
---|
| 327 | uint8_t extboot_signature /* extended boot signature */
|
---|
| 328 | uint32_t serial /* serial number */
|
---|
| 329 | char label[11] /* volume label */
|
---|
| 330 | char fstype[8] /* filesystem type */
|
---|
| 331 | padding[448] /* boot code */
|
---|
| 332 | uint8_t boot_signature[2] /* boot signature */
|
---|
| 333 | """
|
---|
| 334 |
|
---|
[8bc6fcf] | 335 | EMPTY_SECTOR = """little:
|
---|
[04619ba] | 336 | padding[512] /* empty sector data */
|
---|
| 337 | """
|
---|
| 338 |
|
---|
| 339 | FAT_ENTRY = """little:
|
---|
| 340 | uint16_t next /* FAT16 entry */
|
---|
[8bc6fcf] | 341 | """
|
---|
| 342 |
|
---|
[5749372] | 343 | def usage(prname):
|
---|
| 344 | "Print usage syntax"
|
---|
[28f4adb] | 345 | print(prname + " <EXTRA_BYTES> <PATH> <IMAGE>")
|
---|
[5749372] | 346 |
|
---|
| 347 | def main():
|
---|
[14f2100] | 348 | if (len(sys.argv) < 4):
|
---|
[5749372] | 349 | usage(sys.argv[0])
|
---|
| 350 | return
|
---|
| 351 |
|
---|
[14f2100] | 352 | if (not sys.argv[1].isdigit()):
|
---|
[28f4adb] | 353 | print("<EXTRA_BYTES> must be a number")
|
---|
[14f2100] | 354 | return
|
---|
| 355 |
|
---|
| 356 | extra_bytes = int(sys.argv[1])
|
---|
| 357 |
|
---|
| 358 | path = os.path.abspath(sys.argv[2])
|
---|
[5749372] | 359 | if (not os.path.isdir(path)):
|
---|
[28f4adb] | 360 | print("<PATH> must be a directory")
|
---|
[5749372] | 361 | return
|
---|
| 362 |
|
---|
[c4702798] | 363 | fat16_clusters = 4096
|
---|
[04619ba] | 364 |
|
---|
[700eaa9] | 365 | sector_size = 512
|
---|
| 366 | cluster_size = 4096
|
---|
[04619ba] | 367 | dirent_size = 32
|
---|
| 368 | fatent_size = 2
|
---|
| 369 | fat_count = 2
|
---|
| 370 | reserved_clusters = 2
|
---|
| 371 |
|
---|
| 372 | # Make sure the filesystem is large enought for FAT16
|
---|
[14f2100] | 373 | size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
|
---|
[28f4adb] | 374 | while (size // cluster_size < fat16_clusters):
|
---|
[ab579fa] | 375 | if (cluster_size > sector_size):
|
---|
[28f4adb] | 376 | cluster_size = cluster_size // 2
|
---|
[14f2100] | 377 | size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
|
---|
[04619ba] | 378 | else:
|
---|
| 379 | size = fat16_clusters * cluster_size + reserved_clusters * cluster_size
|
---|
[700eaa9] | 380 |
|
---|
[04619ba] | 381 | root_size = align_up(root_entries(path) * dirent_size, cluster_size)
|
---|
[700eaa9] | 382 |
|
---|
[28f4adb] | 383 | fat_size = align_up(align_up(size, cluster_size) // cluster_size * fatent_size, sector_size)
|
---|
[04619ba] | 384 |
|
---|
[28f4adb] | 385 | sectors = (cluster_size + fat_count * fat_size + root_size + size) // sector_size
|
---|
[04619ba] | 386 | root_start = cluster_size + fat_count * fat_size
|
---|
| 387 | data_start = root_start + root_size
|
---|
[700eaa9] | 388 |
|
---|
[28f4adb] | 389 | outf = open(sys.argv[3], "wb")
|
---|
[5749372] | 390 |
|
---|
| 391 | boot_sector = xstruct.create(BOOT_SECTOR)
|
---|
| 392 | boot_sector.jmp = [0xEB, 0x3C, 0x90]
|
---|
[28f4adb] | 393 | boot_sector.oem = b'MSDOS5.0'
|
---|
[700eaa9] | 394 | boot_sector.sector = sector_size
|
---|
[28f4adb] | 395 | boot_sector.cluster = cluster_size // sector_size
|
---|
| 396 | boot_sector.reserved = cluster_size // sector_size
|
---|
[04619ba] | 397 | boot_sector.fats = fat_count
|
---|
[28f4adb] | 398 | boot_sector.rootdir = root_size // dirent_size
|
---|
[de4a1cf] | 399 | if (sectors <= 65535):
|
---|
| 400 | boot_sector.sectors = sectors
|
---|
| 401 | else:
|
---|
| 402 | boot_sector.sectors = 0
|
---|
[5749372] | 403 | boot_sector.descriptor = 0xF8
|
---|
[28f4adb] | 404 | boot_sector.fat_sectors = fat_size // sector_size
|
---|
[700eaa9] | 405 | boot_sector.track_sectors = 63
|
---|
| 406 | boot_sector.heads = 6
|
---|
[5749372] | 407 | boot_sector.hidden = 0
|
---|
[de4a1cf] | 408 | if (sectors > 65535):
|
---|
| 409 | boot_sector.sectors_big = sectors
|
---|
| 410 | else:
|
---|
| 411 | boot_sector.sectors_big = 0
|
---|
[5749372] | 412 |
|
---|
[700eaa9] | 413 | boot_sector.drive = 0x80
|
---|
[5749372] | 414 | boot_sector.extboot_signature = 0x29
|
---|
[0951495] | 415 | boot_sector.serial = random.randint(0, 0x7fffffff)
|
---|
[28f4adb] | 416 | boot_sector.label = b'HELENOS'
|
---|
| 417 | boot_sector.fstype = b'FAT16 '
|
---|
[5749372] | 418 | boot_sector.boot_signature = [0x55, 0xAA]
|
---|
| 419 |
|
---|
| 420 | outf.write(boot_sector.pack())
|
---|
| 421 |
|
---|
[8bc6fcf] | 422 | empty_sector = xstruct.create(EMPTY_SECTOR)
|
---|
| 423 |
|
---|
[04619ba] | 424 | # Reserved sectors
|
---|
[28f4adb] | 425 | for i in range(1, cluster_size // sector_size):
|
---|
[8bc6fcf] | 426 | outf.write(empty_sector.pack())
|
---|
| 427 |
|
---|
| 428 | # FAT tables
|
---|
[04619ba] | 429 | for i in range(0, fat_count):
|
---|
[28f4adb] | 430 | for j in range(0, fat_size // sector_size):
|
---|
[8bc6fcf] | 431 | outf.write(empty_sector.pack())
|
---|
| 432 |
|
---|
| 433 | # Root directory
|
---|
[28f4adb] | 434 | for i in range(0, root_size // sector_size):
|
---|
[8bc6fcf] | 435 | outf.write(empty_sector.pack())
|
---|
| 436 |
|
---|
| 437 | # Data
|
---|
[28f4adb] | 438 | for i in range(0, size // sector_size):
|
---|
[8bc6fcf] | 439 | outf.write(empty_sector.pack())
|
---|
| 440 |
|
---|
[28f4adb] | 441 | fat = array.array('L', [0] * (fat_size // fatent_size))
|
---|
[04619ba] | 442 | fat[0] = 0xfff8
|
---|
| 443 | fat[1] = 0xffff
|
---|
| 444 |
|
---|
[95b730c2] | 445 | recursion(True, path, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, 0)
|
---|
[04619ba] | 446 |
|
---|
| 447 | # Store FAT
|
---|
| 448 | fat_entry = xstruct.create(FAT_ENTRY)
|
---|
| 449 | for i in range(0, fat_count):
|
---|
| 450 | outf.seek(cluster_size + i * fat_size)
|
---|
[28f4adb] | 451 | for j in range(0, fat_size // fatent_size):
|
---|
[04619ba] | 452 | fat_entry.next = fat[j]
|
---|
| 453 | outf.write(fat_entry.pack())
|
---|
| 454 |
|
---|
[5749372] | 455 | outf.close()
|
---|
| 456 |
|
---|
| 457 | if __name__ == '__main__':
|
---|
| 458 | main()
|
---|