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