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