Rev 3264 | Rev 3511 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3264 | Rev 3509 | ||
---|---|---|---|
Line 33... | Line 33... | ||
33 | import sys |
33 | import sys |
34 | import os |
34 | import os |
35 | import random |
35 | import random |
36 | import xstruct |
36 | import xstruct |
37 | 37 | ||
- | 38 | def align_up(size, alignment): |
|
- | 39 | "Return size aligned up to alignment" |
|
- | 40 | ||
- | 41 | if (size % alignment == 0): |
|
- | 42 | return size |
|
- | 43 | ||
- | 44 | return (((size / alignment) + 1) * alignment) |
|
- | 45 | ||
- | 46 | def subtree_size(root, cluster_size): |
|
- | 47 | "Recursive directory walk and calculate size" |
|
- | 48 | ||
- | 49 | size = 0 |
|
- | 50 | files = 0 |
|
- | 51 | ||
- | 52 | for name in os.listdir(root): |
|
- | 53 | canon = os.path.join(root, name) |
|
- | 54 | ||
- | 55 | if (os.path.isfile(canon)): |
|
- | 56 | size += align_up(os.path.getsize(canon), cluster_size) |
|
- | 57 | files += 1 |
|
- | 58 | ||
- | 59 | if (os.path.isdir(canon)): |
|
- | 60 | size += subtree_size(canon, cluster_size) |
|
- | 61 | files += 1 |
|
- | 62 | ||
- | 63 | return size + align_up(files * 32, cluster_size) |
|
- | 64 | ||
- | 65 | def root_entries(root): |
|
- | 66 | "Return number of root directory entries" |
|
- | 67 | ||
- | 68 | return len(os.listdir(root)) |
|
- | 69 | ||
38 | BOOT_SECTOR = """little: |
70 | BOOT_SECTOR = """little: |
39 | uint8_t jmp[3] /* jump instruction */ |
71 | uint8_t jmp[3] /* jump instruction */ |
40 | char oem[8] /* OEM string */ |
72 | char oem[8] /* OEM string */ |
41 | uint16_t sector /* bytes per sector */ |
73 | uint16_t sector /* bytes per sector */ |
42 | uint8_t cluster /* sectors per cluster */ |
74 | uint8_t cluster /* sectors per cluster */ |
Line 74... | Line 106... | ||
74 | path = os.path.abspath(sys.argv[1]) |
106 | path = os.path.abspath(sys.argv[1]) |
75 | if (not os.path.isdir(path)): |
107 | if (not os.path.isdir(path)): |
76 | print "<PATH> must be a directory" |
108 | print "<PATH> must be a directory" |
77 | return |
109 | return |
78 | 110 | ||
- | 111 | sector_size = 512 |
|
- | 112 | cluster_size = 4096 |
|
- | 113 | ||
- | 114 | root_size = align_up(root_entries(sys.argv[1]) * 32, sector_size) |
|
- | 115 | size = subtree_size(sys.argv[1], cluster_size) |
|
- | 116 | fat_size = align_up(size / cluster_size * 2, sector_size) |
|
- | 117 | ||
- | 118 | sectors = (cluster_size + 2 * fat_size + root_size + size) / sector_size |
|
- | 119 | ||
79 | outf = file(sys.argv[2], "w") |
120 | outf = file(sys.argv[2], "w") |
80 | 121 | ||
81 | boot_sector = xstruct.create(BOOT_SECTOR) |
122 | boot_sector = xstruct.create(BOOT_SECTOR) |
82 | boot_sector.jmp = [0xEB, 0x3C, 0x90] |
123 | boot_sector.jmp = [0xEB, 0x3C, 0x90] |
83 | boot_sector.oem = "MSDOS5.0" |
124 | boot_sector.oem = "MSDOS5.0" |
84 | boot_sector.sector = 512 |
125 | boot_sector.sector = sector_size |
85 | boot_sector.cluster = 8 # 4096 bytes per cluster |
126 | boot_sector.cluster = cluster_size / sector_size |
86 | boot_sector.reserved = 1 |
127 | boot_sector.reserved = cluster_size / sector_size |
87 | boot_sector.fats = 2 |
128 | boot_sector.fats = 2 |
88 | boot_sector.rootdir = 224 # FIXME: root directory should be sector aligned |
129 | boot_sector.rootdir = root_size / 32 |
89 | boot_sector.sectors = 0 # FIXME |
130 | boot_sector.sectors = (sectors if (sectors <= 65535) else 0) |
90 | boot_sector.descriptor = 0xF8 |
131 | boot_sector.descriptor = 0xF8 |
91 | boot_sector.fat_sectors = 0 # FIXME |
132 | boot_sector.fat_sectors = fat_size / sector_size |
92 | boot_sector.track_sectors = 0 # FIXME |
133 | boot_sector.track_sectors = 63 |
93 | boot_sector.heads = 0 # FIXME |
134 | boot_sector.heads = 6 |
94 | boot_sector.hidden = 0 |
135 | boot_sector.hidden = 0 |
95 | boot_sector.sectors_big = 0 # FIXME |
136 | boot_sector.sectors_big = (sectors if (sectors > 65535) else 0) |
96 | 137 | ||
97 | boot_sector.drive = 0 |
138 | boot_sector.drive = 0x80 |
98 | boot_sector.extboot_signature = 0x29 |
139 | boot_sector.extboot_signature = 0x29 |
99 | boot_sector.serial = random.randint(0, 0xFFFFFFFF) |
140 | boot_sector.serial = random.randint(0, 0xFFFFFFFF) |
100 | boot_sector.label = "HELENOS" |
141 | boot_sector.label = "HELENOS" |
101 | boot_sector.fstype = "FAT16 " |
142 | boot_sector.fstype = "FAT16 " |
102 | boot_sector.boot_signature = [0x55, 0xAA] |
143 | boot_sector.boot_signature = [0x55, 0xAA] |