Rev 3424 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3424 | Rev 3425 | ||
---|---|---|---|
Line 30... | Line 30... | ||
30 | TMPFS creator |
30 | TMPFS creator |
31 | """ |
31 | """ |
32 | 32 | ||
33 | import sys |
33 | import sys |
34 | import os |
34 | import os |
35 | import struct |
35 | import xstruct |
36 | 36 | ||
- | 37 | HEADER = """little: |
|
- | 38 | char tag[5] /* 'TMPFS' */ |
|
- | 39 | """ |
|
- | 40 | ||
37 | def align_up(size, alignment): |
41 | DENTRY_NONE = """little: |
- | 42 | uint8_t kind /* NONE */ |
|
- | 43 | uint32_t fname_len /* 0 */ |
|
- | 44 | """ |
|
- | 45 | ||
- | 46 | DENTRY_FILE = """little: |
|
- | 47 | uint8_t kind /* FILE */ |
|
- | 48 | uint32_t fname_len /* filename length */ |
|
38 | "Align upwards to a given alignment" |
49 | char fname[%d] /* filename */ |
- | 50 | uint32_t flen /* file length */ |
|
- | 51 | """ |
|
- | 52 | ||
- | 53 | DENTRY_DIRECTORY = """little: |
|
- | 54 | uint8_t kind /* DIRECTORY */ |
|
39 | return (((size) + ((alignment) - 1)) & ~((alignment) - 1)) |
55 | uint32_t fname_len /* filename length */ |
- | 56 | char fname[%d] /* filename */ |
|
- | 57 | """ |
|
- | 58 | ||
- | 59 | TMPFS_NONE = 0 |
|
- | 60 | TMPFS_FILE = 1 |
|
- | 61 | TMPFS_DIRECTORY = 2 |
|
40 | 62 | ||
41 | def usage(prname): |
63 | def usage(prname): |
42 | "Print usage syntax" |
64 | "Print usage syntax" |
43 | print prname + " <ALIGNMENT> <PATH> <IMAGE>" |
65 | print prname + " <PATH> <IMAGE>" |
44 | 66 | ||
45 | def recursion(root, outf): |
67 | def recursion(root, outf): |
46 | "Recursive directory walk" |
68 | "Recursive directory walk" |
47 | 69 | ||
48 | payload_size = 0 |
- | |
49 | - | ||
50 | for name in os.listdir(root): |
70 | for name in os.listdir(root): |
51 | canon = os.path.join(root, name) |
71 | canon = os.path.join(root, name) |
52 | 72 | ||
53 | if (os.path.isfile(canon)): |
73 | if (os.path.isfile(canon)): |
54 | outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 1, len(name), name)) |
- | |
55 | payload_size += 5 + len(name) |
- | |
56 | size = os.path.getsize(canon) |
74 | size = os.path.getsize(canon) |
57 | rd = 0; |
75 | |
58 | outf.write(struct.pack("<L", size)) |
76 | dentry = xstruct.create(DENTRY_FILE % len(name)) |
- | 77 | dentry.kind = TMPFS_FILE |
|
- | 78 | dentry.fname_len = len(name) |
|
59 | payload_size += 4 |
79 | dentry.fname = name |
- | 80 | dentry.flen = size |
|
- | 81 | ||
- | 82 | outf.write(dentry.pack()) |
|
60 | 83 | ||
61 | inf = file(canon, "r") |
84 | inf = file(canon, "r") |
- | 85 | rd = 0; |
|
62 | while (rd < size): |
86 | while (rd < size): |
63 | data = inf.read(4096); |
87 | data = inf.read(4096); |
64 | outf.write(data) |
88 | outf.write(data) |
65 | payload_size += len(data) |
- | |
66 | rd += len(data) |
89 | rd += len(data) |
67 | inf.close() |
90 | inf.close() |
68 | 91 | ||
69 | if (os.path.isdir(canon)): |
92 | if (os.path.isdir(canon)): |
70 | outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 2, len(name), name)) |
93 | dentry = xstruct.create(DENTRY_DIRECTORY % len(name)) |
- | 94 | dentry.kind = TMPFS_DIRECTORY |
|
71 | payload_size += 5 + len(name) |
95 | dentry.fname_len = len(name) |
- | 96 | dentry.fname = name |
|
- | 97 | ||
- | 98 | outf.write(dentry.pack()) |
|
- | 99 | ||
72 | payload_size += recursion(canon, outf) |
100 | recursion(canon, outf) |
- | 101 | ||
73 | outf.write(struct.pack("<BL", 0, 0)) |
102 | dentry = xstruct.create(DENTRY_NONE) |
- | 103 | dentry.kind = TMPFS_NONE |
|
74 | payload_size += 5 |
104 | dentry.fname_len = 0 |
75 | 105 | ||
76 | return payload_size |
106 | outf.write(dentry.pack()) |
77 | 107 | ||
78 | def main(): |
108 | def main(): |
79 | if (len(sys.argv) < 4): |
109 | if (len(sys.argv) < 3): |
80 | usage(sys.argv[0]) |
110 | usage(sys.argv[0]) |
81 | return |
111 | return |
82 | 112 | ||
83 | if (not sys.argv[1].isdigit()): |
- | |
84 | print "<ALIGNMENT> must be a number" |
- | |
85 | return |
- | |
86 | - | ||
87 | align = int(sys.argv[1], 0) |
- | |
88 | path = os.path.abspath(sys.argv[2]) |
113 | path = os.path.abspath(sys.argv[1]) |
89 | if (not os.path.isdir(path)): |
114 | if (not os.path.isdir(path)): |
90 | print "<PATH> must be a directory" |
115 | print "<PATH> must be a directory" |
91 | return |
116 | return |
92 | 117 | ||
93 | header_size = align_up(18, align) |
- | |
94 | outf = file(sys.argv[3], "w") |
118 | outf = file(sys.argv[2], "w") |
95 | outf.write(struct.pack("<" + ("%d" % header_size) + "x")) |
- | |
96 | 119 | ||
97 | outf.write(struct.pack("<5s", "TMPFS")) |
120 | header = xstruct.create(HEADER) |
98 | payload_size = 5 |
121 | header.tag = "TMPFS" |
99 | 122 | ||
100 | payload_size += recursion(path, outf) |
- | |
101 | - | ||
102 | outf.write(struct.pack("<BL", 0, 0)) |
123 | outf.write(header.pack()) |
103 | payload_size += 5 |
- | |
104 | 124 | ||
- | 125 | recursion(path, outf) |
|
- | 126 | ||
105 | aligned_size = align_up(payload_size, align) |
127 | dentry = xstruct.create(DENTRY_NONE) |
- | 128 | dentry.kind = TMPFS_NONE |
|
- | 129 | dentry.fname_len = 0 |
|
- | 130 | ||
- | 131 | outf.write(dentry.pack()) |
|
106 | 132 | ||
107 | if (aligned_size - payload_size > 0): |
- | |
108 | outf.write(struct.pack("<" + ("%d" % (aligned_size - payload_size)) + "x")) |
- | |
109 | - | ||
110 | outf.seek(0) |
- | |
111 | outf.write(struct.pack("<4sBBLQ", "HORD", 1, 1, header_size, aligned_size)) |
- | |
112 | outf.close() |
133 | outf.close() |
113 | 134 | ||
114 | if __name__ == '__main__': |
135 | if __name__ == '__main__': |
115 | main() |
136 | main() |