[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
|
---|
[cc1a727] | 39 | from imgutil import *
|
---|
[700eaa9] | 40 |
|
---|
[04619ba] | 41 | def subtree_size(root, cluster_size, dirent_size):
|
---|
[700eaa9] | 42 | "Recursive directory walk and calculate size"
|
---|
[a35b458] | 43 |
|
---|
[700eaa9] | 44 | size = 0
|
---|
[95b730c2] | 45 | files = 2
|
---|
[a35b458] | 46 |
|
---|
[cc1a727] | 47 | for item in listdir_items(root):
|
---|
| 48 | if item.is_file:
|
---|
| 49 | size += align_up(item.size, cluster_size)
|
---|
[700eaa9] | 50 | files += 1
|
---|
[cc1a727] | 51 | elif item.is_dir:
|
---|
| 52 | size += subtree_size(item.path, cluster_size, dirent_size)
|
---|
[700eaa9] | 53 | files += 1
|
---|
[a35b458] | 54 |
|
---|
[04619ba] | 55 | return size + align_up(files * dirent_size, cluster_size)
|
---|
[700eaa9] | 56 |
|
---|
| 57 | def root_entries(root):
|
---|
| 58 | "Return number of root directory entries"
|
---|
[a35b458] | 59 |
|
---|
[700eaa9] | 60 | return len(os.listdir(root))
|
---|
| 61 |
|
---|
[cc1a727] | 62 | def write_file(item, outf, cluster_size, data_start, fat, reserved_clusters):
|
---|
[04619ba] | 63 | "Store the contents of a file"
|
---|
[a35b458] | 64 |
|
---|
[04619ba] | 65 | prev = -1
|
---|
[95b730c2] | 66 | first = 0
|
---|
[a35b458] | 67 |
|
---|
[cc1a727] | 68 | for data in chunks(item, cluster_size):
|
---|
[04619ba] | 69 | empty_cluster = fat.index(0)
|
---|
| 70 | fat[empty_cluster] = 0xffff
|
---|
[a35b458] | 71 |
|
---|
[04619ba] | 72 | if (prev != -1):
|
---|
| 73 | fat[prev] = empty_cluster
|
---|
| 74 | else:
|
---|
| 75 | first = empty_cluster
|
---|
[a35b458] | 76 |
|
---|
[04619ba] | 77 | prev = empty_cluster
|
---|
[a35b458] | 78 |
|
---|
[95b730c2] | 79 | outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size)
|
---|
[04619ba] | 80 | outf.write(data)
|
---|
[a35b458] | 81 |
|
---|
[cc1a727] | 82 | return first, item.size
|
---|
[04619ba] | 83 |
|
---|
[95b730c2] | 84 | def write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster):
|
---|
| 85 | "Store the contents of a directory"
|
---|
[a35b458] | 86 |
|
---|
[95b730c2] | 87 | length = len(directory)
|
---|
| 88 | size = length * dirent_size
|
---|
| 89 | prev = -1
|
---|
| 90 | first = 0
|
---|
[a35b458] | 91 |
|
---|
[95b730c2] | 92 | i = 0
|
---|
| 93 | rd = 0;
|
---|
| 94 | while (rd < size):
|
---|
| 95 | if (prev != -1):
|
---|
| 96 | empty_cluster = fat.index(0)
|
---|
| 97 | fat[empty_cluster] = 0xffff
|
---|
| 98 | fat[prev] = empty_cluster
|
---|
| 99 | else:
|
---|
| 100 | first = empty_cluster
|
---|
[a35b458] | 101 |
|
---|
[95b730c2] | 102 | prev = empty_cluster
|
---|
[a35b458] | 103 |
|
---|
[28f4adb] | 104 | data = bytes()
|
---|
[95b730c2] | 105 | data_len = 0
|
---|
| 106 | while ((i < length) and (data_len < cluster_size)):
|
---|
| 107 | if (i == 0):
|
---|
| 108 | directory[i].cluster = empty_cluster
|
---|
[a35b458] | 109 |
|
---|
[95b730c2] | 110 | data += directory[i].pack()
|
---|
| 111 | data_len += dirent_size
|
---|
| 112 | i += 1
|
---|
[a35b458] | 113 |
|
---|
[95b730c2] | 114 | outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size)
|
---|
| 115 | outf.write(data)
|
---|
| 116 | rd += len(data)
|
---|
[a35b458] | 117 |
|
---|
[95b730c2] | 118 | return first, size
|
---|
| 119 |
|
---|
[04619ba] | 120 | DIR_ENTRY = """little:
|
---|
| 121 | char name[8] /* file name */
|
---|
| 122 | char ext[3] /* file extension */
|
---|
| 123 | uint8_t attr /* file attributes */
|
---|
[a248234] | 124 | uint8_t lcase /* file name case (NT extension) */
|
---|
[04619ba] | 125 | uint8_t ctime_fine /* create time (fine resolution) */
|
---|
| 126 | uint16_t ctime /* create time */
|
---|
| 127 | uint16_t cdate /* create date */
|
---|
| 128 | uint16_t adate /* access date */
|
---|
| 129 | padding[2] /* EA-index */
|
---|
| 130 | uint16_t mtime /* modification time */
|
---|
| 131 | uint16_t mdate /* modification date */
|
---|
| 132 | uint16_t cluster /* first cluster */
|
---|
| 133 | uint32_t size /* file size */
|
---|
| 134 | """
|
---|
| 135 |
|
---|
[95b730c2] | 136 | DOT_DIR_ENTRY = """little:
|
---|
| 137 | uint8_t signature /* 0x2e signature */
|
---|
| 138 | char name[7] /* empty */
|
---|
| 139 | char ext[3] /* empty */
|
---|
| 140 | uint8_t attr /* file attributes */
|
---|
| 141 | padding[1] /* reserved for NT */
|
---|
| 142 | uint8_t ctime_fine /* create time (fine resolution) */
|
---|
| 143 | uint16_t ctime /* create time */
|
---|
| 144 | uint16_t cdate /* create date */
|
---|
| 145 | uint16_t adate /* access date */
|
---|
| 146 | padding[2] /* EA-index */
|
---|
| 147 | uint16_t mtime /* modification time */
|
---|
| 148 | uint16_t mdate /* modification date */
|
---|
| 149 | uint16_t cluster /* first cluster */
|
---|
| 150 | uint32_t size /* file size */
|
---|
| 151 | """
|
---|
| 152 |
|
---|
| 153 | DOTDOT_DIR_ENTRY = """little:
|
---|
| 154 | uint8_t signature[2] /* 0x2e signature */
|
---|
| 155 | char name[6] /* empty */
|
---|
| 156 | char ext[3] /* empty */
|
---|
| 157 | uint8_t attr /* file attributes */
|
---|
| 158 | padding[1] /* reserved for NT */
|
---|
| 159 | uint8_t ctime_fine /* create time (fine resolution) */
|
---|
| 160 | uint16_t ctime /* create time */
|
---|
| 161 | uint16_t cdate /* create date */
|
---|
| 162 | uint16_t adate /* access date */
|
---|
| 163 | padding[2] /* EA-index */
|
---|
| 164 | uint16_t mtime /* modification time */
|
---|
| 165 | uint16_t mdate /* modification date */
|
---|
| 166 | uint16_t cluster /* first cluster */
|
---|
| 167 | uint32_t size /* file size */
|
---|
| 168 | """
|
---|
| 169 |
|
---|
[7751ff1] | 170 | LFN_DIR_ENTRY = """little:
|
---|
| 171 | uint8_t seq /* sequence number */
|
---|
| 172 | char name1[10] /* first part of the name */
|
---|
| 173 | uint8_t attr /* attributes */
|
---|
| 174 | uint8_t rec_type /* LFN record type */
|
---|
| 175 | uint8_t checksum /* LFN checksum */
|
---|
| 176 | char name2[12] /* second part of the name */
|
---|
| 177 | uint16_t cluster /* cluster */
|
---|
| 178 | char name3[4] /* third part of the name */
|
---|
[a9a6d8d] | 179 | """
|
---|
| 180 |
|
---|
[7751ff1] | 181 | lchars = set(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
---|
| 182 | 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
---|
| 183 | 'U', 'V', 'W', 'X', 'Y', 'Z',
|
---|
| 184 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
---|
| 185 | '!', '#', '$', '%', '&', '\'', '(', ')', '-', '@',
|
---|
| 186 | '^', '_', '`', '{', '}', '~', '.'])
|
---|
[a9a6d8d] | 187 |
|
---|
[7751ff1] | 188 | def fat_lchars(name):
|
---|
| 189 | "Filter FAT legal characters"
|
---|
[a35b458] | 190 |
|
---|
[67435b1] | 191 | filtered_name = b''
|
---|
[87608a5] | 192 | filtered = False
|
---|
[a35b458] | 193 |
|
---|
[87608a5] | 194 | for char in name.encode('ascii', 'replace').upper():
|
---|
| 195 | if char in lchars:
|
---|
| 196 | filtered_name += char
|
---|
| 197 | else:
|
---|
| 198 | filtered_name += b'_'
|
---|
| 199 | filtered = True
|
---|
[a35b458] | 200 |
|
---|
[87608a5] | 201 | return (filtered_name, filtered)
|
---|
[a9a6d8d] | 202 |
|
---|
[7751ff1] | 203 | def fat_name83(name, name83_list):
|
---|
| 204 | "Create a 8.3 name for the given name"
|
---|
[a35b458] | 205 |
|
---|
[87608a5] | 206 | ascii_name, lfn = fat_lchars(name)
|
---|
[67435b1] | 207 | # Splitting works only on strings, not on bytes
|
---|
| 208 | ascii_parts = ascii_name.decode('utf8').split('.')
|
---|
[a35b458] | 209 |
|
---|
[7751ff1] | 210 | short_name = ''
|
---|
| 211 | short_ext = ''
|
---|
[a35b458] | 212 |
|
---|
[7751ff1] | 213 | if len(ascii_name) > 11:
|
---|
[a9a6d8d] | 214 | lfn = True
|
---|
[a35b458] | 215 |
|
---|
[7751ff1] | 216 | if len(ascii_parts) > 0:
|
---|
| 217 | short_name = ascii_parts[0]
|
---|
| 218 | if len(short_name) > 8:
|
---|
[a9a6d8d] | 219 | lfn = True
|
---|
[a35b458] | 220 |
|
---|
[7751ff1] | 221 | if len(ascii_parts) > 1:
|
---|
| 222 | short_ext = ascii_parts[-1]
|
---|
| 223 | if len(short_ext) > 3:
|
---|
[a9a6d8d] | 224 | lfn = True
|
---|
[a35b458] | 225 |
|
---|
[7751ff1] | 226 | if len(ascii_parts) > 2:
|
---|
[a9a6d8d] | 227 | lfn = True
|
---|
[a35b458] | 228 |
|
---|
[7751ff1] | 229 | if lfn == False:
|
---|
| 230 | name83_list.append(short_name + '.' + short_ext)
|
---|
| 231 | return (short_name.ljust(8)[0:8], short_ext.ljust(3)[0:3], False)
|
---|
[a35b458] | 232 |
|
---|
[a9a6d8d] | 233 | # For filenames with multiple extensions, we treat the last one
|
---|
| 234 | # as the actual extension. The rest of the filename is stripped
|
---|
| 235 | # of dots and concatenated to form the short name
|
---|
[7751ff1] | 236 | for part in ascii_parts[1:-1]:
|
---|
| 237 | short_name += part
|
---|
[a35b458] | 238 |
|
---|
[7751ff1] | 239 | for number in range(1, 999999):
|
---|
| 240 | number_str = ('~' + str(number)).upper()
|
---|
[a35b458] | 241 |
|
---|
[7751ff1] | 242 | if len(short_name) + len(number_str) > 8:
|
---|
| 243 | short_name = short_name[0:8 - len(number_str)]
|
---|
[a35b458] | 244 |
|
---|
[7751ff1] | 245 | short_name += number_str;
|
---|
[a35b458] | 246 |
|
---|
[7751ff1] | 247 | if not (short_name + '.' + short_ext) in name83_list:
|
---|
[a9a6d8d] | 248 | break
|
---|
[a35b458] | 249 |
|
---|
[7751ff1] | 250 | name83_list.append(short_name + '.' + short_ext)
|
---|
| 251 | return (short_name.ljust(8)[0:8], short_ext.ljust(3)[0:3], True)
|
---|
[a9a6d8d] | 252 |
|
---|
[7751ff1] | 253 | def create_lfn_dirent(name, seq, checksum):
|
---|
| 254 | "Create LFN directory entry"
|
---|
[a35b458] | 255 |
|
---|
[7751ff1] | 256 | entry = xstruct.create(LFN_DIR_ENTRY)
|
---|
| 257 | name_rest = name[26:]
|
---|
[a35b458] | 258 |
|
---|
[7751ff1] | 259 | if len(name_rest) > 0:
|
---|
| 260 | entry.seq = seq
|
---|
| 261 | else:
|
---|
| 262 | entry.seq = seq | 0x40
|
---|
[a35b458] | 263 |
|
---|
[7751ff1] | 264 | entry.name1 = name[0:10]
|
---|
| 265 | entry.name2 = name[10:22]
|
---|
| 266 | entry.name3 = name[22:26]
|
---|
[a35b458] | 267 |
|
---|
[7751ff1] | 268 | entry.attr = 0x0F
|
---|
| 269 | entry.rec_type = 0
|
---|
| 270 | entry.checksum = checksum
|
---|
| 271 | entry.cluster = 0
|
---|
[a35b458] | 272 |
|
---|
[7751ff1] | 273 | return (entry, name_rest)
|
---|
[a9a6d8d] | 274 |
|
---|
[7751ff1] | 275 | def lfn_checksum(name):
|
---|
| 276 | "Calculate LFN checksum"
|
---|
[a35b458] | 277 |
|
---|
[7751ff1] | 278 | checksum = 0
|
---|
| 279 | for i in range(0, 11):
|
---|
| 280 | checksum = (((checksum & 1) << 7) + (checksum >> 1) + ord(name[i])) & 0xFF
|
---|
[a35b458] | 281 |
|
---|
[7751ff1] | 282 | return checksum
|
---|
[04619ba] | 283 |
|
---|
[7751ff1] | 284 | def create_dirent(name, name83_list, directory, cluster, size):
|
---|
| 285 | short_name, short_ext, lfn = fat_name83(name, name83_list)
|
---|
[a35b458] | 286 |
|
---|
[a9a6d8d] | 287 | dir_entry = xstruct.create(DIR_ENTRY)
|
---|
[a35b458] | 288 |
|
---|
[7751ff1] | 289 | dir_entry.name = short_name
|
---|
| 290 | dir_entry.ext = short_ext
|
---|
[a35b458] | 291 |
|
---|
[04619ba] | 292 | if (directory):
|
---|
| 293 | dir_entry.attr = 0x30
|
---|
| 294 | else:
|
---|
| 295 | dir_entry.attr = 0x20
|
---|
[a35b458] | 296 |
|
---|
[a248234] | 297 | dir_entry.lcase = 0x18
|
---|
[04619ba] | 298 | dir_entry.ctime_fine = 0 # FIXME
|
---|
| 299 | dir_entry.ctime = 0 # FIXME
|
---|
| 300 | dir_entry.cdate = 0 # FIXME
|
---|
| 301 | dir_entry.adate = 0 # FIXME
|
---|
| 302 | dir_entry.mtime = 0 # FIXME
|
---|
| 303 | dir_entry.mdate = 0 # FIXME
|
---|
| 304 | dir_entry.cluster = cluster
|
---|
[a35b458] | 305 |
|
---|
[95b730c2] | 306 | if (directory):
|
---|
| 307 | dir_entry.size = 0
|
---|
| 308 | else:
|
---|
| 309 | dir_entry.size = size
|
---|
[a35b458] | 310 |
|
---|
[a9a6d8d] | 311 | if not lfn:
|
---|
| 312 | return [dir_entry]
|
---|
[a35b458] | 313 |
|
---|
[7751ff1] | 314 | long_name = name.encode('utf_16_le')
|
---|
| 315 | entries = [dir_entry]
|
---|
[a35b458] | 316 |
|
---|
[7751ff1] | 317 | seq = 1
|
---|
| 318 | checksum = lfn_checksum(dir_entry.name + dir_entry.ext)
|
---|
[a35b458] | 319 |
|
---|
[7751ff1] | 320 | while len(long_name) > 0:
|
---|
| 321 | long_entry, long_name = create_lfn_dirent(long_name, seq, checksum)
|
---|
| 322 | entries.append(long_entry)
|
---|
| 323 | seq += 1
|
---|
[a35b458] | 324 |
|
---|
[7751ff1] | 325 | entries.reverse()
|
---|
[a9a6d8d] | 326 | return entries
|
---|
[95b730c2] | 327 |
|
---|
| 328 | def create_dot_dirent(empty_cluster):
|
---|
| 329 | dir_entry = xstruct.create(DOT_DIR_ENTRY)
|
---|
[a35b458] | 330 |
|
---|
[95b730c2] | 331 | dir_entry.signature = 0x2e
|
---|
[432f68a] | 332 | dir_entry.name = b' '
|
---|
| 333 | dir_entry.ext = b' '
|
---|
[95b730c2] | 334 | dir_entry.attr = 0x10
|
---|
[a35b458] | 335 |
|
---|
[95b730c2] | 336 | dir_entry.ctime_fine = 0 # FIXME
|
---|
| 337 | dir_entry.ctime = 0 # FIXME
|
---|
| 338 | dir_entry.cdate = 0 # FIXME
|
---|
| 339 | dir_entry.adate = 0 # FIXME
|
---|
| 340 | dir_entry.mtime = 0 # FIXME
|
---|
| 341 | dir_entry.mdate = 0 # FIXME
|
---|
| 342 | dir_entry.cluster = empty_cluster
|
---|
| 343 | dir_entry.size = 0
|
---|
[a35b458] | 344 |
|
---|
[95b730c2] | 345 | return dir_entry
|
---|
| 346 |
|
---|
| 347 | def create_dotdot_dirent(parent_cluster):
|
---|
| 348 | dir_entry = xstruct.create(DOTDOT_DIR_ENTRY)
|
---|
[a35b458] | 349 |
|
---|
[95b730c2] | 350 | dir_entry.signature = [0x2e, 0x2e]
|
---|
[432f68a] | 351 | dir_entry.name = b' '
|
---|
| 352 | dir_entry.ext = b' '
|
---|
[95b730c2] | 353 | dir_entry.attr = 0x10
|
---|
[a35b458] | 354 |
|
---|
[95b730c2] | 355 | dir_entry.ctime_fine = 0 # FIXME
|
---|
| 356 | dir_entry.ctime = 0 # FIXME
|
---|
| 357 | dir_entry.cdate = 0 # FIXME
|
---|
| 358 | dir_entry.adate = 0 # FIXME
|
---|
| 359 | dir_entry.mtime = 0 # FIXME
|
---|
| 360 | dir_entry.mdate = 0 # FIXME
|
---|
| 361 | dir_entry.cluster = parent_cluster
|
---|
| 362 | dir_entry.size = 0
|
---|
[a35b458] | 363 |
|
---|
[04619ba] | 364 | return dir_entry
|
---|
| 365 |
|
---|
[95b730c2] | 366 | def recursion(head, root, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, parent_cluster):
|
---|
[04619ba] | 367 | "Recursive directory walk"
|
---|
[a35b458] | 368 |
|
---|
[04619ba] | 369 | directory = []
|
---|
[7751ff1] | 370 | name83_list = []
|
---|
[a35b458] | 371 |
|
---|
[7751ff1] | 372 | if not head:
|
---|
[95b730c2] | 373 | # Directory cluster preallocation
|
---|
| 374 | empty_cluster = fat.index(0)
|
---|
[7751ff1] | 375 | fat[empty_cluster] = 0xFFFF
|
---|
[a35b458] | 376 |
|
---|
[95b730c2] | 377 | directory.append(create_dot_dirent(empty_cluster))
|
---|
| 378 | directory.append(create_dotdot_dirent(parent_cluster))
|
---|
| 379 | else:
|
---|
| 380 | empty_cluster = 0
|
---|
[a35b458] | 381 |
|
---|
[7751ff1] | 382 | for item in listdir_items(root):
|
---|
[cc1a727] | 383 | if item.is_file:
|
---|
| 384 | rv = write_file(item, outf, cluster_size, data_start, fat, reserved_clusters)
|
---|
[7751ff1] | 385 | directory.extend(create_dirent(item.name, name83_list, False, rv[0], rv[1]))
|
---|
[cc1a727] | 386 | elif item.is_dir:
|
---|
| 387 | rv = recursion(False, item.path, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
|
---|
[7751ff1] | 388 | directory.extend(create_dirent(item.name, name83_list, True, rv[0], rv[1]))
|
---|
[a35b458] | 389 |
|
---|
[7751ff1] | 390 | if head:
|
---|
[04619ba] | 391 | outf.seek(root_start)
|
---|
| 392 | for dir_entry in directory:
|
---|
| 393 | outf.write(dir_entry.pack())
|
---|
[95b730c2] | 394 | else:
|
---|
| 395 | return write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
|
---|
[04619ba] | 396 |
|
---|
[5749372] | 397 | BOOT_SECTOR = """little:
|
---|
| 398 | uint8_t jmp[3] /* jump instruction */
|
---|
| 399 | char oem[8] /* OEM string */
|
---|
| 400 | uint16_t sector /* bytes per sector */
|
---|
| 401 | uint8_t cluster /* sectors per cluster */
|
---|
| 402 | uint16_t reserved /* reserved sectors */
|
---|
| 403 | uint8_t fats /* number of FATs */
|
---|
| 404 | uint16_t rootdir /* root directory entries */
|
---|
| 405 | uint16_t sectors /* total number of sectors */
|
---|
| 406 | uint8_t descriptor /* media descriptor */
|
---|
| 407 | uint16_t fat_sectors /* sectors per single FAT */
|
---|
| 408 | uint16_t track_sectors /* sectors per track */
|
---|
| 409 | uint16_t heads /* number of heads */
|
---|
| 410 | uint32_t hidden /* hidden sectors */
|
---|
| 411 | uint32_t sectors_big /* total number of sectors (if sectors == 0) */
|
---|
[a35b458] | 412 |
|
---|
[5749372] | 413 | /* Extended BIOS Parameter Block */
|
---|
| 414 | uint8_t drive /* physical drive number */
|
---|
| 415 | padding[1] /* reserved (current head) */
|
---|
| 416 | uint8_t extboot_signature /* extended boot signature */
|
---|
| 417 | uint32_t serial /* serial number */
|
---|
| 418 | char label[11] /* volume label */
|
---|
| 419 | char fstype[8] /* filesystem type */
|
---|
| 420 | padding[448] /* boot code */
|
---|
| 421 | uint8_t boot_signature[2] /* boot signature */
|
---|
| 422 | """
|
---|
| 423 |
|
---|
[8bc6fcf] | 424 | EMPTY_SECTOR = """little:
|
---|
[04619ba] | 425 | padding[512] /* empty sector data */
|
---|
| 426 | """
|
---|
| 427 |
|
---|
| 428 | FAT_ENTRY = """little:
|
---|
| 429 | uint16_t next /* FAT16 entry */
|
---|
[8bc6fcf] | 430 | """
|
---|
| 431 |
|
---|
[5749372] | 432 | def usage(prname):
|
---|
| 433 | "Print usage syntax"
|
---|
[28f4adb] | 434 | print(prname + " <EXTRA_BYTES> <PATH> <IMAGE>")
|
---|
[5749372] | 435 |
|
---|
| 436 | def main():
|
---|
[14f2100] | 437 | if (len(sys.argv) < 4):
|
---|
[5749372] | 438 | usage(sys.argv[0])
|
---|
| 439 | return
|
---|
[a35b458] | 440 |
|
---|
[14f2100] | 441 | if (not sys.argv[1].isdigit()):
|
---|
[28f4adb] | 442 | print("<EXTRA_BYTES> must be a number")
|
---|
[14f2100] | 443 | return
|
---|
[a35b458] | 444 |
|
---|
[14f2100] | 445 | extra_bytes = int(sys.argv[1])
|
---|
[a35b458] | 446 |
|
---|
[67435b1] | 447 | path = os.path.abspath(sys.argv[2])
|
---|
[5749372] | 448 | if (not os.path.isdir(path)):
|
---|
[28f4adb] | 449 | print("<PATH> must be a directory")
|
---|
[5749372] | 450 | return
|
---|
[a35b458] | 451 |
|
---|
[c4702798] | 452 | fat16_clusters = 4096
|
---|
[a35b458] | 453 |
|
---|
[700eaa9] | 454 | sector_size = 512
|
---|
| 455 | cluster_size = 4096
|
---|
[04619ba] | 456 | dirent_size = 32
|
---|
| 457 | fatent_size = 2
|
---|
| 458 | fat_count = 2
|
---|
| 459 | reserved_clusters = 2
|
---|
[a35b458] | 460 |
|
---|
[f4057f5] | 461 | # Make sure the filesystem is large enough for FAT16
|
---|
[14f2100] | 462 | size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
|
---|
[28f4adb] | 463 | while (size // cluster_size < fat16_clusters):
|
---|
[ab579fa] | 464 | if (cluster_size > sector_size):
|
---|
[28f4adb] | 465 | cluster_size = cluster_size // 2
|
---|
[14f2100] | 466 | size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
|
---|
[04619ba] | 467 | else:
|
---|
| 468 | size = fat16_clusters * cluster_size + reserved_clusters * cluster_size
|
---|
[a35b458] | 469 |
|
---|
[04619ba] | 470 | root_size = align_up(root_entries(path) * dirent_size, cluster_size)
|
---|
[a35b458] | 471 |
|
---|
[28f4adb] | 472 | fat_size = align_up(align_up(size, cluster_size) // cluster_size * fatent_size, sector_size)
|
---|
[a35b458] | 473 |
|
---|
[28f4adb] | 474 | sectors = (cluster_size + fat_count * fat_size + root_size + size) // sector_size
|
---|
[04619ba] | 475 | root_start = cluster_size + fat_count * fat_size
|
---|
| 476 | data_start = root_start + root_size
|
---|
[a35b458] | 477 |
|
---|
[28f4adb] | 478 | outf = open(sys.argv[3], "wb")
|
---|
[a35b458] | 479 |
|
---|
[5749372] | 480 | boot_sector = xstruct.create(BOOT_SECTOR)
|
---|
| 481 | boot_sector.jmp = [0xEB, 0x3C, 0x90]
|
---|
[28f4adb] | 482 | boot_sector.oem = b'MSDOS5.0'
|
---|
[700eaa9] | 483 | boot_sector.sector = sector_size
|
---|
[28f4adb] | 484 | boot_sector.cluster = cluster_size // sector_size
|
---|
| 485 | boot_sector.reserved = cluster_size // sector_size
|
---|
[04619ba] | 486 | boot_sector.fats = fat_count
|
---|
[28f4adb] | 487 | boot_sector.rootdir = root_size // dirent_size
|
---|
[de4a1cf] | 488 | if (sectors <= 65535):
|
---|
| 489 | boot_sector.sectors = sectors
|
---|
| 490 | else:
|
---|
| 491 | boot_sector.sectors = 0
|
---|
[5749372] | 492 | boot_sector.descriptor = 0xF8
|
---|
[28f4adb] | 493 | boot_sector.fat_sectors = fat_size // sector_size
|
---|
[700eaa9] | 494 | boot_sector.track_sectors = 63
|
---|
| 495 | boot_sector.heads = 6
|
---|
[5749372] | 496 | boot_sector.hidden = 0
|
---|
[de4a1cf] | 497 | if (sectors > 65535):
|
---|
| 498 | boot_sector.sectors_big = sectors
|
---|
| 499 | else:
|
---|
| 500 | boot_sector.sectors_big = 0
|
---|
[a35b458] | 501 |
|
---|
[700eaa9] | 502 | boot_sector.drive = 0x80
|
---|
[5749372] | 503 | boot_sector.extboot_signature = 0x29
|
---|
[0951495] | 504 | boot_sector.serial = random.randint(0, 0x7fffffff)
|
---|
[28f4adb] | 505 | boot_sector.label = b'HELENOS'
|
---|
| 506 | boot_sector.fstype = b'FAT16 '
|
---|
[5749372] | 507 | boot_sector.boot_signature = [0x55, 0xAA]
|
---|
[a35b458] | 508 |
|
---|
[5749372] | 509 | outf.write(boot_sector.pack())
|
---|
[a35b458] | 510 |
|
---|
[8bc6fcf] | 511 | empty_sector = xstruct.create(EMPTY_SECTOR)
|
---|
[a35b458] | 512 |
|
---|
[04619ba] | 513 | # Reserved sectors
|
---|
[28f4adb] | 514 | for i in range(1, cluster_size // sector_size):
|
---|
[8bc6fcf] | 515 | outf.write(empty_sector.pack())
|
---|
[a35b458] | 516 |
|
---|
[8bc6fcf] | 517 | # FAT tables
|
---|
[04619ba] | 518 | for i in range(0, fat_count):
|
---|
[28f4adb] | 519 | for j in range(0, fat_size // sector_size):
|
---|
[8bc6fcf] | 520 | outf.write(empty_sector.pack())
|
---|
[a35b458] | 521 |
|
---|
[8bc6fcf] | 522 | # Root directory
|
---|
[28f4adb] | 523 | for i in range(0, root_size // sector_size):
|
---|
[8bc6fcf] | 524 | outf.write(empty_sector.pack())
|
---|
[a35b458] | 525 |
|
---|
[8bc6fcf] | 526 | # Data
|
---|
[28f4adb] | 527 | for i in range(0, size // sector_size):
|
---|
[8bc6fcf] | 528 | outf.write(empty_sector.pack())
|
---|
[a35b458] | 529 |
|
---|
[28f4adb] | 530 | fat = array.array('L', [0] * (fat_size // fatent_size))
|
---|
[04619ba] | 531 | fat[0] = 0xfff8
|
---|
| 532 | fat[1] = 0xffff
|
---|
[a35b458] | 533 |
|
---|
[95b730c2] | 534 | recursion(True, path, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, 0)
|
---|
[a35b458] | 535 |
|
---|
[04619ba] | 536 | # Store FAT
|
---|
| 537 | fat_entry = xstruct.create(FAT_ENTRY)
|
---|
| 538 | for i in range(0, fat_count):
|
---|
| 539 | outf.seek(cluster_size + i * fat_size)
|
---|
[28f4adb] | 540 | for j in range(0, fat_size // fatent_size):
|
---|
[04619ba] | 541 | fat_entry.next = fat[j]
|
---|
| 542 | outf.write(fat_entry.pack())
|
---|
[a35b458] | 543 |
|
---|
[5749372] | 544 | outf.close()
|
---|
[7751ff1] | 545 |
|
---|
[5749372] | 546 | if __name__ == '__main__':
|
---|
| 547 | main()
|
---|