Subversion Repositories HelenOS

Rev

Rev 3262 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3262 Rev 3263
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 xstruct
35
import xstruct
36
 
36
 
37
HEADER = """little:
37
HEADER = """little:
38
    char[4] tag            /* 'HORD' */
38
    char tag[4]            /* 'HORD' */
39
    uint8_t version        /* version */
39
    uint8_t version        /* version */
40
    uint8_t encoding       /* encoding */
40
    uint8_t encoding       /* encoding */
41
    uint32_t header_size   /* header size */
41
    uint32_t header_size   /* header size */
42
    uint64_t payload_size  /* payload size */
42
    uint64_t payload_size  /* payload size */
43
"""
43
"""
44
 
44
 
45
HORD_LSB = 1
45
HORD_LSB = 1
46
 
46
 
47
def align_up(size, alignment):
47
def align_up(size, alignment):
48
    "Align upwards to a given alignment"
48
    "Align upwards to a given alignment"
49
    return (((size) + ((alignment) - 1)) & ~((alignment) - 1))
49
    return (((size) + ((alignment) - 1)) & ~((alignment) - 1))
50
 
50
 
51
def usage(prname):
51
def usage(prname):
52
    "Print usage syntax"
52
    "Print usage syntax"
53
    print prname + " <ALIGNMENT> <FS_IMAGE> <HORD_IMAGE>"
53
    print prname + " <ALIGNMENT> <FS_IMAGE> <HORD_IMAGE>"
54
 
54
 
55
def main():
55
def main():
56
    if (len(sys.argv) < 4):
56
    if (len(sys.argv) < 4):
57
        usage(sys.argv[0])
57
        usage(sys.argv[0])
58
        return
58
        return
59
   
59
   
60
    if (not sys.argv[1].isdigit()):
60
    if (not sys.argv[1].isdigit()):
61
        print "<ALIGNMENT> must be a number"
61
        print "<ALIGNMENT> must be a number"
62
        return
62
        return
63
   
63
   
64
    align = int(sys.argv[1], 0)
64
    align = int(sys.argv[1], 0)
65
    if (align <= 0):
65
    if (align <= 0):
66
        print "<ALIGNMENT> must be positive"
66
        print "<ALIGNMENT> must be positive"
67
        return
67
        return
68
   
68
   
69
    fs_image = os.path.abspath(sys.argv[2])
69
    fs_image = os.path.abspath(sys.argv[2])
70
    if (not os.path.isfile(fs_image)):
70
    if (not os.path.isfile(fs_image)):
71
        print "<FS_IMAGE> must be a file"
71
        print "<FS_IMAGE> must be a file"
72
        return
72
        return
73
   
73
   
74
    inf = file(fs_image, "rb")
74
    inf = file(fs_image, "rb")
75
    outf = file(sys.argv[3], "wb")
75
    outf = file(sys.argv[3], "wb")
76
   
76
   
77
    header = xstruct.create(HEADER)
77
    header = xstruct.create(HEADER)
78
   
78
   
79
    header_size = header.size()
79
    header_size = header.size()
80
    payload_size = os.path.getsize(fs_image)
80
    payload_size = os.path.getsize(fs_image)
81
   
81
   
82
    header_size_aligned = align_up(header_size, align)
82
    header_size_aligned = align_up(header_size, align)
83
    payload_size_aligned = align_up(payload_size, align)
83
    payload_size_aligned = align_up(payload_size, align)
84
   
84
   
85
    header.tag = "HORD"
85
    header.tag = "HORD"
86
    header.version = 1
86
    header.version = 1
87
    header.encoding = HORD_LSB
87
    header.encoding = HORD_LSB
88
    header.header_size = header_size_aligned
88
    header.header_size = header_size_aligned
89
    header.payload_size = payload_size_aligned
89
    header.payload_size = payload_size_aligned
90
   
90
   
91
    outf.write(header.pack())
91
    outf.write(header.pack())
92
    outf.write(xstruct.create("little: padding[%d]" % (header_size_aligned - header_size)).pack())
92
    outf.write(xstruct.create("little: padding[%d]" % (header_size_aligned - header_size)).pack())
93
   
93
   
94
    outf.write(inf.read())
94
    outf.write(inf.read())
95
   
95
   
96
    padding = payload_size_aligned - payload_size
96
    padding = payload_size_aligned - payload_size
97
    if (padding > 0):
97
    if (padding > 0):
98
        outf.write(xstruct.create("little: padding[%d]" % padding).pack())
98
        outf.write(xstruct.create("little: padding[%d]" % padding).pack())
99
   
99
   
100
    inf.close()
100
    inf.close()
101
    outf.close()
101
    outf.close()
102
 
102
 
103
if __name__ == '__main__':
103
if __name__ == '__main__':
104
    main()
104
    main()
105
 
105