Subversion Repositories HelenOS

Rev

Rev 3259 | Rev 3262 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3259 Rev 3260
1
#!/usr/bin/env python
1
#!/usr/bin/env python
2
#
2
#
3
# Copyright (c) 2008 Martin Decky
3
# Copyright (c) 2008 Martin Decky
4
# All rights reserved.
4
# All rights reserved.
5
#
5
#
6
# Redistribution and use in source and binary forms, with or without
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
7
# modification, are permitted provided that the following conditions
8
# are met:
8
# are met:
9
#
9
#
10
# - Redistributions of source code must retain the above copyright
10
# - Redistributions of source code must retain the above copyright
11
#   notice, this list of conditions and the following disclaimer.
11
#   notice, this list of conditions and the following disclaimer.
12
# - Redistributions in binary form must reproduce the above copyright
12
# - Redistributions in binary form must reproduce the above copyright
13
#   notice, this list of conditions and the following disclaimer in the
13
#   notice, this list of conditions and the following disclaimer in the
14
#   documentation and/or other materials provided with the distribution.
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
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.
16
#   derived from this software without specific prior written permission.
17
#
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#
28
#
29
"""
29
"""
30
HORD encapsulator
30
HORD encapsulator
31
"""
31
"""
32
 
32
 
33
import sys
33
import sys
34
import os
34
import os
35
import struct
35
import struct
-
 
36
import xstruct
-
 
37
 
-
 
38
HEADER = xstruct.convert("little: "
-
 
39
    "char[4]   /* 'HORD' */ "
-
 
40
    "uint8_t   /* version */ "
-
 
41
    "uint8_t   /* encoding */ "
-
 
42
    "uint32_t  /* header size */ "
-
 
43
    "uint64_t  /* payload size */ "
-
 
44
)
-
 
45
 
-
 
46
HORD_VERSION = 1
-
 
47
HORD_LSB = 1
36
 
48
 
37
def align_up(size, alignment):
49
def align_up(size, alignment):
38
    "Align upwards to a given alignment"
50
    "Align upwards to a given alignment"
39
    return (((size) + ((alignment) - 1)) & ~((alignment) - 1))
51
    return (((size) + ((alignment) - 1)) & ~((alignment) - 1))
40
 
52
 
41
def usage(prname):
53
def usage(prname):
42
    "Print usage syntax"
54
    "Print usage syntax"
43
    print prname + " <ALIGNMENT> <FS_IMAGE> <HORD_IMAGE>"
55
    print prname + " <ALIGNMENT> <FS_IMAGE> <HORD_IMAGE>"
44
 
56
 
45
def main():
57
def main():
46
    if (len(sys.argv) < 4):
58
    if (len(sys.argv) < 4):
47
        usage(sys.argv[0])
59
        usage(sys.argv[0])
48
        return
60
        return
49
   
61
   
50
    if (not sys.argv[1].isdigit()):
62
    if (not sys.argv[1].isdigit()):
51
        print "<ALIGNMENT> must be a number"
63
        print "<ALIGNMENT> must be a number"
52
        return
64
        return
53
   
65
   
54
    align = int(sys.argv[1], 0)
66
    align = int(sys.argv[1], 0)
-
 
67
    if (align <= 0):
-
 
68
        print "<ALIGNMENT> must be positive"
-
 
69
        return
-
 
70
   
55
    fs_image = os.path.abspath(sys.argv[2])
71
    fs_image = os.path.abspath(sys.argv[2])
56
    if (not os.path.isfile(fs_image)):
72
    if (not os.path.isfile(fs_image)):
57
        print "<FS_IMAGE> must be a file"
73
        print "<FS_IMAGE> must be a file"
58
        return
74
        return
59
   
75
   
60
    inf = file(fs_image, "rb")
76
    inf = file(fs_image, "rb")
61
    outf = file(sys.argv[3], "wb")
77
    outf = file(sys.argv[3], "wb")
62
   
78
   
-
 
79
    header_size = struct.calcsize(HEADER)
-
 
80
    payload_size = os.path.getsize(fs_image)
-
 
81
   
63
    header_size = align_up(18, align)
82
    header_size_aligned = align_up(header_size, align)
64
    aligned_size = align_up(os.path.getsize(fs_image), align)
83
    payload_size_aligned = align_up(payload_size, align)
65
   
84
   
66
    outf.write(struct.pack("<4sBBLQ", "HORD", 1, 1, header_size, aligned_size))
85
    outf.write(struct.pack(HEADER, "HORD", HORD_VERSION, HORD_LSB, header_size_aligned, payload_size_aligned))
67
    outf.write(struct.pack("<" + ("%d" % (header_size - 18)) + "x"))
86
    outf.write(xstruct.little_padding(header_size_aligned - header_size))
68
   
87
   
69
    outf.write(inf.read())
88
    outf.write(inf.read())
70
   
89
   
71
    padding = aligned_size - os.path.getsize(fs_image)
90
    padding = payload_size_aligned - payload_size
72
    if (padding > 0):
91
    if (padding > 0):
73
        outf.write(struct.pack("<" + ("%d" % padding) + "x"))
92
        outf.write(xstruct.little_padding(padding))
74
   
93
   
75
    inf.close()
94
    inf.close()
76
    outf.close()
95
    outf.close()
77
 
96
 
78
if __name__ == '__main__':
97
if __name__ == '__main__':
79
    main()
98
    main()
80
 
99