blob: 1dd15b74ce322b6e393d5fcf4bc77b0770767a55 [file] [log] [blame]
Casey Dahlin29e2b212016-09-01 18:07:15 -07001#!/usr/bin/env python
2#
3# Copyright 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import os
18import sys
19import struct
20
21FAT_TABLE_START = 0x200
22DEL_MARKER = 0xe5
23ESCAPE_DEL_MARKER = 0x05
24
25ATTRIBUTE_READ_ONLY = 0x1
26ATTRIBUTE_HIDDEN = 0x2
27ATTRIBUTE_SYSTEM = 0x4
28ATTRIBUTE_VOLUME_LABEL = 0x8
29ATTRIBUTE_SUBDIRECTORY = 0x10
30ATTRIBUTE_ARCHIVE = 0x20
31ATTRIBUTE_DEVICE = 0x40
32
33LFN_ATTRIBUTES = \
34 ATTRIBUTE_VOLUME_LABEL | \
35 ATTRIBUTE_SYSTEM | \
36 ATTRIBUTE_HIDDEN | \
37 ATTRIBUTE_READ_ONLY
38LFN_ATTRIBUTES_BYTE = struct.pack("B", LFN_ATTRIBUTES)
39
40MAX_CLUSTER_ID = 0x7FFF
41
42def read_le_short(f):
43 "Read a little-endian 2-byte integer from the given file-like object"
44 return struct.unpack("<H", f.read(2))[0]
45
46def read_le_long(f):
47 "Read a little-endian 4-byte integer from the given file-like object"
48 return struct.unpack("<L", f.read(4))[0]
49
50def read_byte(f):
51 "Read a 1-byte integer from the given file-like object"
52 return struct.unpack("B", f.read(1))[0]
53
54def skip_bytes(f, n):
55 "Fast-forward the given file-like object by n bytes"
56 f.seek(n, os.SEEK_CUR)
57
58def skip_short(f):
59 "Fast-forward the given file-like object 2 bytes"
60 skip_bytes(f, 2)
61
62def skip_byte(f):
63 "Fast-forward the given file-like object 1 byte"
64 skip_bytes(f, 1)
65
66def rewind_bytes(f, n):
67 "Rewind the given file-like object n bytes"
68 skip_bytes(f, -n)
69
70def rewind_short(f):
71 "Rewind the given file-like object 2 bytes"
72 rewind_bytes(f, 2)
73
74class fake_file(object):
75 """
76 Interface for python file-like objects that we use to manipulate the image.
77 Inheritors must have an idx member which indicates the file pointer, and a
78 size member which indicates the total file size.
79 """
80
81 def seek(self, amount, direction=0):
82 "Implementation of seek from python's file-like object interface."
83 if direction == os.SEEK_CUR:
84 self.idx += amount
85 elif direction == os.SEEK_END:
86 self.idx = self.size - amount
87 else:
88 self.idx = amount
89
90 if self.idx < 0:
91 self.idx = 0
92 if self.idx > self.size:
93 self.idx = self.size
94
95class fat_file(fake_file):
96 """
97 A file inside of our fat image. The file may or may not have a dentry, and
98 if it does this object knows nothing about it. All we see is a valid cluster
99 chain.
100 """
101
102 def __init__(self, fs, cluster, size=None):
103 """
104 fs: The fat() object for the image this file resides in.
105 cluster: The first cluster of data for this file.
106 size: The size of this file. If not given, we use the total length of the
107 cluster chain that starts from the cluster argument.
108 """
109 self.fs = fs
110 self.start_cluster = cluster
111 self.size = size
112
113 if self.size is None:
114 self.size = fs.get_chain_size(cluster)
115
116 self.idx = 0
117
118 def read(self, size):
119 "Read method for pythonic file-like interface."
120 if self.idx + size > self.size:
121 size = self.size - self.idx
122 got = self.fs.read_file(self.start_cluster, self.idx, size)
123 self.idx += len(got)
124 return got
125
126 def write(self, data):
127 "Write method for pythonic file-like interface."
128 self.fs.write_file(self.start_cluster, self.idx, data)
129 self.idx += len(data)
130
131 if self.idx > self.size:
132 self.size = self.idx
133
134def shorten(name, index):
135 """
136 Create a file short name from the given long name (with the extension already
137 removed). The index argument gives a disambiguating integer to work into the
138 name to avoid collisions.
139 """
140 name = "".join(name.split('.')).upper()
141 postfix = "~" + str(index)
142 return name[:8 - len(postfix)] + postfix
143
144class fat_dir(object):
145 "A directory in our fat filesystem."
146
147 def __init__(self, backing):
148 """
149 backing: A file-like object from which we can read dentry info. Should have
150 an fs member allowing us to get to the underlying image.
151 """
152 self.backing = backing
153 self.dentries = []
154 to_read = self.backing.size / 32
155
156 self.backing.seek(0)
157
158 while to_read > 0:
159 (dent, consumed) = self.backing.fs.read_dentry(self.backing)
160 to_read -= consumed
161
162 if dent:
163 self.dentries.append(dent)
164
165 def __str__(self):
166 return "\n".join([str(x) for x in self.dentries]) + "\n"
167
168 def add_dentry(self, attributes, shortname, ext, longname, first_cluster,
169 size):
170 """
171 Add a new dentry to this directory.
172 attributes: Attribute flags for this dentry. See the ATTRIBUTE_ constants
173 above.
174 shortname: Short name of this file. Up to 8 characters, no dots.
175 ext: Extension for this file. Up to 3 characters, no dots.
176 longname: The long name for this file, with extension. Largely unrestricted.
177 first_cluster: The first cluster in the cluster chain holding the contents
178 of this file.
179 size: The size of this file. Set to 0 for subdirectories.
180 """
181 new_dentry = dentry(self.backing.fs, attributes, shortname, ext,
182 longname, first_cluster, size)
183 new_dentry.commit(self.backing)
184 self.dentries.append(new_dentry)
185 return new_dentry
186
187 def make_short_name(self, name):
188 """
189 Given a long file name, return an 8.3 short name as a tuple. Name will be
190 engineered not to collide with other such names in this folder.
191 """
192 parts = name.rsplit('.', 1)
193
194 if len(parts) == 1:
195 parts.append('')
196
197 name = parts[0]
198 ext = parts[1].upper()
199
200 index = 1
201 shortened = shorten(name, index)
202
203 for dent in self.dentries:
204 assert dent.longname != name, "File must not exist"
205 if dent.shortname == shortened:
206 index += 1
207 shortened = shorten(name, index)
208
209 if len(name) <= 8 and len(ext) <= 3 and not '.' in name:
210 return (name.upper().ljust(8), ext.ljust(3))
211
212 return (shortened.ljust(8), ext[:3].ljust(3))
213
214 def new_file(self, name, data=None):
215 """
216 Add a new regular file to this directory.
217 name: The name of the new file.
218 data: The contents of the new file. Given as a file-like object.
219 """
220 size = 0
221 if data:
222 data.seek(0, os.SEEK_END)
223 size = data.tell()
224
225 chunk = self.backing.fs.allocate(size or 1)
226 (shortname, ext) = self.make_short_name(name)
227 self.add_dentry(0, shortname, ext, name, chunk, size)
228
229 if data is None:
230 return
231
232 data_file = fat_file(self.backing.fs, chunk, size)
233 data.seek(0)
234 data_file.write(data.read())
235
236 def new_subdirectory(self, name):
237 """
238 Create a new subdirectory of this directory with the given name.
239 Returns a fat_dir().
240 """
241 chunk = self.backing.fs.allocate(1)
242 (shortname, ext) = self.make_short_name(name)
243 new_dentry = dentry(self.backing.fs, ATTRIBUTE_SUBDIRECTORY,
244 shortname, ext, name, chunk, 0)
245 new_dentry.commit(self.backing)
246 return new_dentry.open_directory()
247
248def lfn_checksum(name_data):
249 """
250 Given the characters of an 8.3 file name (concatenated *without* the dot),
251 Compute a one-byte checksum which needs to appear in corresponding long file
252 name entries.
253 """
254 assert len(name_data) == 11, "Name data should be exactly 11 characters"
255 name_data = struct.unpack("B" * 11, name_data)
256
257 result = 0
258
259 for char in name_data:
260 last_bit = (result & 1) << 7
261 result = (result >> 1) | last_bit
262 result += char
263 result = result & 0xFF
264
265 return struct.pack("B", result)
266
267class dentry(object):
268 "A directory entry"
269 def __init__(self, fs, attributes, shortname, ext, longname,
270 first_cluster, size):
271 """
272 fs: The fat() object for the image we're stored in.
273 attributes: The attribute flags for this dentry. See the ATTRIBUTE_ flags
274 above.
275 shortname: The short name stored in this dentry. Up to 8 characters, no
276 dots.
277 ext: The file extension stored in this dentry. Up to 3 characters, no
278 dots.
279 longname: The long file name stored in this dentry.
280 first_cluster: The first cluster in the cluster chain backing the file
281 this dentry points to.
282 size: Size of the file this dentry points to. 0 for subdirectories.
283 """
284 self.fs = fs
285 self.attributes = attributes
286 self.shortname = shortname
287 self.ext = ext
288 self.longname = longname
289 self.first_cluster = first_cluster
290 self.size = size
291
292 def name(self):
293 "A friendly text file name for this dentry."
294 if self.longname:
295 return self.longname
296
297 if not self.ext or len(self.ext) == 0:
298 return self.shortname
299
300 return self.shortname + "." + self.ext
301
302 def __str__(self):
303 return self.name() + " (" + str(self.size) + \
304 " bytes @ " + str(self.first_cluster) + ")"
305
306 def is_directory(self):
307 "Return whether this dentry points to a directory."
308 return (self.attributes & ATTRIBUTE_SUBDIRECTORY) != 0
309
310 def open_file(self):
311 "Open the target of this dentry if it is a regular file."
312 assert not self.is_directory(), "Cannot open directory as file"
313 return fat_file(self.fs, self.first_cluster, self.size)
314
315 def open_directory(self):
316 "Open the target of this dentry if it is a directory."
317 assert self.is_directory(), "Cannot open file as directory"
318 return fat_dir(fat_file(self.fs, self.first_cluster))
319
320 def longname_records(self, checksum):
321 """
322 Get the longname records necessary to store this dentry's long name,
323 packed as a series of 32-byte strings.
324 """
325 if self.longname is None:
326 return []
327 if len(self.longname) == 0:
328 return []
329
330 encoded_long_name = self.longname.encode('utf-16-le')
331 long_name_padding = "\0" * (26 - (len(encoded_long_name) % 26))
332 padded_long_name = encoded_long_name + long_name_padding
333
334 chunks = [padded_long_name[i:i+26] for i in range(0,
335 len(padded_long_name), 26)]
336 records = []
337 sequence_number = 1
338
339 for c in chunks:
340 sequence_byte = struct.pack("B", sequence_number)
341 sequence_number += 1
342 record = sequence_byte + c[:10] + LFN_ATTRIBUTES_BYTE + "\0" + \
343 checksum + c[10:22] + "\0\0" + c[22:]
344 records.append(record)
345
346 last = records.pop()
347 last_seq = struct.unpack("B", last[0])[0]
348 last_seq = last_seq | 0x40
349 last = struct.pack("B", last_seq) + last[1:]
350 records.append(last)
351 records.reverse()
352
353 return records
354
355 def commit(self, f):
356 """
357 Write this dentry into the given file-like object,
358 which is assumed to contain a FAT directory.
359 """
360 f.seek(0)
361 padded_short_name = self.shortname.ljust(8)
362 padded_ext = self.ext.ljust(3)
363 name_data = padded_short_name + padded_ext
364 longname_record_data = self.longname_records(lfn_checksum(name_data))
365 record = struct.pack("<11sBBBHHHHHHHL",
366 name_data,
367 self.attributes,
368 0,
369 0,
370 0,
371 0,
372 0,
373 0,
374 0,
375 0,
376 self.first_cluster,
377 self.size)
378 entry = "".join(longname_record_data + [record])
379
380 record_count = len(longname_record_data) + 1
381
382 found_count = 0
383
384 while True:
385 record = f.read(32)
386
387 if record is None or len(record) != 32:
388 break
389
390 marker = struct.unpack("B", record[0])[0]
391
392 if marker == DEL_MARKER or marker == 0:
393 found_count += 1
394
395 if found_count == record_count:
396 break
397 else:
398 found_count = 0
399
400 if found_count != record_count:
401 f.write("\0" * self.fs.bytes_per_cluster)
402 f.seek(-self.fs.bytes_per_cluster, os.SEEK_CUR)
403 else:
404 f.seek(-(record_count * 32), os.SEEK_CUR)
405 f.write(entry)
406
407class root_dentry_file(fake_file):
408 """
409 File-like object for the root directory. The root directory isn't stored in a
410 normal file, so we can't use a normal fat_file object to create a view of it.
411 """
412 def __init__(self, fs):
413 self.fs = fs
414 self.idx = 0
415 self.size = fs.root_entries * 32
416
417 def read(self, count):
418 f = self.fs.f
419 f.seek(self.fs.data_start() + self.idx)
420
421 if self.idx + count > self.size:
422 count = self.size - self.idx
423
424 ret = f.read(count)
425 self.idx += len(ret)
426 return ret
427
428 def write(self, data):
429 f = self.fs.f
430 f.seek(self.fs.data_start() + self.idx)
431
432 if self.idx + len(data) > self.size:
433 data = data[:self.size - self.idx]
434
435 f.write(data)
436 self.idx += len(data)
437 if self.idx > self.size:
438 self.size = self.idx
439
440class fat(object):
441 "A FAT image"
442
443 def __init__(self, path):
444 """
445 path: Path to an image file containing a FAT file system.
446 """
447 f = open(path, "r+b")
448
449 self.f = f
450
451 f.seek(0xb)
452 bytes_per_sector = read_le_short(f)
453 sectors_per_cluster = read_byte(f)
454
455 self.bytes_per_cluster = bytes_per_sector * sectors_per_cluster
456
457 reserved_sectors = read_le_short(f)
458 assert reserved_sectors == 1, \
459 "Can only handle FAT with 1 reserved sector"
460
461 fat_count = read_byte(f)
462 assert fat_count == 2, "Can only handle FAT with 2 tables"
463
464 self.root_entries = read_le_short(f)
465
466 skip_short(f) # Image size. Sort of. Useless field.
467 skip_byte(f) # Media type. We don't care.
468
469 self.fat_size = read_le_short(f) * bytes_per_sector
470 self.root = fat_dir(root_dentry_file(self))
471
472 def data_start(self):
473 """
474 Index of the first byte after the FAT tables.
475 """
476 return FAT_TABLE_START + self.fat_size * 2
477
478 def get_chain_size(self, head_cluster):
479 """
480 Return how many total bytes are in the cluster chain rooted at the given
481 cluster.
482 """
483 if head_cluster == 0:
484 return 0
485
486 f = self.f
487 f.seek(FAT_TABLE_START + head_cluster * 2)
488
489 cluster_count = 0
490
491 while head_cluster <= MAX_CLUSTER_ID:
492 cluster_count += 1
493 head_cluster = read_le_short(f)
494 f.seek(FAT_TABLE_START + head_cluster * 2)
495
496 return cluster_count * self.bytes_per_cluster
497
498 def read_dentry(self, f=None):
499 """
500 Read and decode a dentry from the given file-like object at its current
501 seek position.
502 """
503 f = f or self.f
504 attributes = None
505
506 consumed = 1
507
508 lfn_entries = {}
509
510 while True:
511 skip_bytes(f, 11)
512 attributes = read_byte(f)
513 rewind_bytes(f, 12)
514
515 if attributes & LFN_ATTRIBUTES != LFN_ATTRIBUTES:
516 break
517
518 consumed += 1
519
520 seq = read_byte(f)
521 chars = f.read(10)
522 skip_bytes(f, 3) # Various hackish nonsense
523 chars += f.read(12)
524 skip_short(f) # Lots more nonsense
525 chars += f.read(4)
526
527 chars = unicode(chars, "utf-16-le").encode("utf-8")
528
529 lfn_entries[seq] = chars
530
531 ind = read_byte(f)
532
533 if ind == 0 or ind == DEL_MARKER:
534 skip_bytes(f, 31)
535 return (None, consumed)
536
537 if ind == ESCAPE_DEL_MARKER:
538 ind = DEL_MARKER
539
540 ind = str(unichr(ind))
541
542 if ind == '.':
543 skip_bytes(f, 31)
544 return (None, consumed)
545
546 shortname = ind + f.read(7).rstrip()
547 ext = f.read(3).rstrip()
548 skip_bytes(f, 15) # Assorted flags, ctime/atime/mtime, etc.
549 first_cluster = read_le_short(f)
550 size = read_le_long(f)
551
552 lfn = lfn_entries.items()
553 lfn.sort(key=lambda x: x[0])
554 lfn = reduce(lambda x, y: x + y[1], lfn, "")
555
556 if len(lfn) == 0:
557 lfn = None
558 else:
559 lfn = lfn.split('\0', 1)[0]
560
561 return (dentry(self, attributes, shortname, ext, lfn, first_cluster,
562 size), consumed)
563
564 def read_file(self, head_cluster, start_byte, size):
565 """
566 Read from a given FAT file.
567 head_cluster: The first cluster in the file.
568 start_byte: How many bytes in to the file to begin the read.
569 size: How many bytes to read.
570 """
571 f = self.f
572
573 assert size >= 0, "Can't read a negative amount"
574 if size == 0:
575 return ""
576
577 got_data = ""
578
579 while True:
580 size_now = size
581 if start_byte + size > self.bytes_per_cluster:
582 size_now = self.bytes_per_cluster - start_byte
583
584 if start_byte < self.bytes_per_cluster:
585 size -= size_now
586
587 cluster_bytes_from_root = (head_cluster - 2) * \
588 self.bytes_per_cluster
589 bytes_from_root = cluster_bytes_from_root + start_byte
590 bytes_from_data_start = bytes_from_root + self.root_entries * 32
591
592 f.seek(self.data_start() + bytes_from_data_start)
593 line = f.read(size_now)
594 got_data += line
595
596 if size == 0:
597 return got_data
598
599 start_byte -= self.bytes_per_cluster
600
601 if start_byte < 0:
602 start_byte = 0
603
604 f.seek(FAT_TABLE_START + head_cluster * 2)
605 assert head_cluster <= MAX_CLUSTER_ID, "Out-of-bounds read"
606 head_cluster = read_le_short(f)
607 assert head_cluster > 0, "Read free cluster"
608
609 return got_data
610
611 def write_cluster_entry(self, entry):
612 """
613 Write a cluster entry to the FAT table. Assumes our backing file is already
614 seeked to the correct entry in the first FAT table.
615 """
616 f = self.f
617 f.write(struct.pack("<H", entry))
618 skip_bytes(f, self.fat_size - 2)
619 f.write(struct.pack("<H", entry))
620 rewind_bytes(f, self.fat_size)
621
622 def allocate(self, amount):
623 """
624 Allocate a new cluster chain big enough to hold at least the given amount
625 of bytes.
626 """
627 f = self.f
628 f.seek(FAT_TABLE_START + 4)
629
630 current = None
631 current_size = 0
632 free_zones = {}
633
634 pos = 2
635 while pos < self.fat_size / 2:
636 data = read_le_short(f)
637
638 if data == 0 and current is not None:
639 current_size += 1
640 elif data == 0:
641 current = pos
642 current_size = 1
643 elif current is not None:
644 free_zones[current] = current_size
645 current = None
646
647 pos += 1
648
649 if current is not None:
650 free_zones[current] = current_size
651
652 free_zones = free_zones.items()
653 free_zones.sort(key=lambda x: x[1])
654
655 grabbed_zones = []
656 grabbed = 0
657
658 while grabbed < amount and len(free_zones) > 0:
659 zone = free_zones.pop()
660 grabbed += zone[1] * self.bytes_per_cluster
661 grabbed_zones.append(zone)
662
663 if grabbed < amount:
664 return None
665
666 excess = (grabbed - amount) / self.bytes_per_cluster
667
668 grabbed_zones[-1] = (grabbed_zones[-1][0],
669 grabbed_zones[-1][1] - excess)
670
671 out = None
672 grabbed_zones.reverse()
673
674 for cluster, size in grabbed_zones:
675 entries = range(cluster + 1, cluster + size)
676 entries.append(out or 0xFFFF)
677 out = cluster
678 f.seek(FAT_TABLE_START + cluster * 2)
679 for entry in entries:
680 self.write_cluster_entry(entry)
681
682 return out
683
684 def extend_cluster(self, cluster, amount):
685 """
686 Given a cluster which is the *last* cluster in a chain, extend it to hold
687 at least `amount` more bytes.
688 """
689 return_cluster = None
690 f = self.f
691
692 position = FAT_TABLE_START + cluster * 2
693 f.seek(position)
694
695 assert read_le_short(f) == 0xFFFF, "Extending from middle of chain"
696 rewind_short(f)
697
698 while position + 2 < FAT_TABLE_START + self.fat_size and amount > 0:
699 skip_short(f)
700 got = read_le_short(f)
701 rewind_short(f)
702 rewind_short(f)
703
704 if got != 0:
705 break
706
707 cluster += 1
708 return_cluster = return_cluster or cluster
709 position += 2
710 self.write_cluster_entry(cluster)
711
712 if amount < 0:
713 self.write_cluster_entry(0xFFFF)
714 return return_cluster
715
716 new_chunk = self.allocate(amount)
717 f.seek(FAT_TABLE_START + cluster * 2)
718 self.write_cluster_entry(new_chunk)
719
720 return return_cluster or new_chunk
721
722 def write_file(self, head_cluster, start_byte, data):
723 """
724 Write to a given FAT file.
725
726 head_cluster: The first cluster in the file.
727 start_byte: How many bytes in to the file to begin the write.
728 data: The data to write.
729 """
730 f = self.f
731
732 while True:
733 if start_byte < self.bytes_per_cluster:
734 to_write = data[:self.bytes_per_cluster - start_byte]
735 data = data[self.bytes_per_cluster - start_byte:]
736
737 cluster_bytes_from_root = (head_cluster - 2) * \
738 self.bytes_per_cluster
739 bytes_from_root = cluster_bytes_from_root + start_byte
740 bytes_from_data_start = bytes_from_root + self.root_entries * 32
741
742 f.seek(self.data_start() + bytes_from_data_start)
743 f.write(to_write)
744
745 if len(data) == 0:
746 return
747
748 start_byte -= self.bytes_per_cluster
749
750 if start_byte < 0:
751 start_byte = 0
752
753 f.seek(FAT_TABLE_START + head_cluster * 2)
754 next_cluster = read_le_short(f)
755 if next_cluster > MAX_CLUSTER_ID:
756 head_cluster = self.extend_cluster(head_cluster, len(data))
757 else:
758 head_cluster = next_cluster
759 assert head_cluster > 0, "Cannot write free cluster"
760
761def add_item(directory, item):
762 """
763 Copy a file into the given FAT directory. If the path given is a directory,
764 copy recursively.
765 directory: fat_dir to copy the file in to
766 item: Path of local file to copy
767 """
768 if os.path.isdir(item):
769 base = os.path.basename(item)
770 if len(base) == 0:
771 base = os.path.basename(item[:-1])
772 sub = directory.new_subdirectory(base)
773 for next_item in os.listdir(item):
774 add_item(sub, os.path.join(item, next_item))
775 else:
776 with open(item, 'rb') as f:
777 directory.new_file(os.path.basename(item), f)
778
779if __name__ == "__main__":
780 if len(sys.argv) < 3:
781 print("Usage: fat16copy.py <image> <file> [<file> ...]")
782 print("Files are copied into the root of the image.")
783 print("Directories are copied recursively")
784 sys.exit(1)
785
786 root = fat(sys.argv[1]).root
787
788 for p in sys.argv[2:]:
789 add_item(root, p)