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 | #
|
---|
29 | """
|
---|
30 | FAT creator
|
---|
31 | """
|
---|
32 |
|
---|
33 | import sys
|
---|
34 | import os
|
---|
35 | import random
|
---|
36 | import xstruct
|
---|
37 |
|
---|
38 | BOOT_SECTOR = """little:
|
---|
39 | uint8_t jmp[3] /* jump instruction */
|
---|
40 | char oem[8] /* OEM string */
|
---|
41 | uint16_t sector /* bytes per sector */
|
---|
42 | uint8_t cluster /* sectors per cluster */
|
---|
43 | uint16_t reserved /* reserved sectors */
|
---|
44 | uint8_t fats /* number of FATs */
|
---|
45 | uint16_t rootdir /* root directory entries */
|
---|
46 | uint16_t sectors /* total number of sectors */
|
---|
47 | uint8_t descriptor /* media descriptor */
|
---|
48 | uint16_t fat_sectors /* sectors per single FAT */
|
---|
49 | uint16_t track_sectors /* sectors per track */
|
---|
50 | uint16_t heads /* number of heads */
|
---|
51 | uint32_t hidden /* hidden sectors */
|
---|
52 | uint32_t sectors_big /* total number of sectors (if sectors == 0) */
|
---|
53 |
|
---|
54 | /* Extended BIOS Parameter Block */
|
---|
55 | uint8_t drive /* physical drive number */
|
---|
56 | padding[1] /* reserved (current head) */
|
---|
57 | uint8_t extboot_signature /* extended boot signature */
|
---|
58 | uint32_t serial /* serial number */
|
---|
59 | char label[11] /* volume label */
|
---|
60 | char fstype[8] /* filesystem type */
|
---|
61 | padding[448] /* boot code */
|
---|
62 | uint8_t boot_signature[2] /* boot signature */
|
---|
63 | """
|
---|
64 |
|
---|
65 | def usage(prname):
|
---|
66 | "Print usage syntax"
|
---|
67 | print prname + " <PATH> <IMAGE>"
|
---|
68 |
|
---|
69 | def main():
|
---|
70 | if (len(sys.argv) < 3):
|
---|
71 | usage(sys.argv[0])
|
---|
72 | return
|
---|
73 |
|
---|
74 | path = os.path.abspath(sys.argv[1])
|
---|
75 | if (not os.path.isdir(path)):
|
---|
76 | print "<PATH> must be a directory"
|
---|
77 | return
|
---|
78 |
|
---|
79 | outf = file(sys.argv[2], "w")
|
---|
80 |
|
---|
81 | boot_sector = xstruct.create(BOOT_SECTOR)
|
---|
82 | boot_sector.jmp = [0xEB, 0x3C, 0x90]
|
---|
83 | boot_sector.oem = "MSDOS5.0"
|
---|
84 | boot_sector.sector = 512
|
---|
85 | boot_sector.cluster = 8 # 4096 bytes per cluster
|
---|
86 | boot_sector.reserved = 1
|
---|
87 | boot_sector.fats = 2
|
---|
88 | boot_sector.rootdir = 224 # FIXME: root directory should be sector aligned
|
---|
89 | boot_sector.sectors = 0 # FIXME
|
---|
90 | boot_sector.descriptor = 0xF8
|
---|
91 | boot_sector.fat_sectors = 0 # FIXME
|
---|
92 | boot_sector.track_sectors = 0 # FIXME
|
---|
93 | boot_sector.heads = 0 # FIXME
|
---|
94 | boot_sector.hidden = 0
|
---|
95 | boot_sector.sectors_big = 0 # FIXME
|
---|
96 |
|
---|
97 | boot_sector.drive = 0
|
---|
98 | boot_sector.extboot_signature = 0x29
|
---|
99 | boot_sector.serial = random.randint(0, 0xFFFFFFFF)
|
---|
100 | boot_sector.label = "HELENOS"
|
---|
101 | boot_sector.fstype = "FAT16 "
|
---|
102 | boot_sector.boot_signature = [0x55, 0xAA]
|
---|
103 |
|
---|
104 | outf.write(boot_sector.pack())
|
---|
105 |
|
---|
106 | outf.close()
|
---|
107 |
|
---|
108 | if __name__ == '__main__':
|
---|
109 | main()
|
---|