| 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 | """ | 
|---|
| 31 | HORD encapsulator | 
|---|
| 32 | """ | 
|---|
| 33 |  | 
|---|
| 34 | import sys | 
|---|
| 35 | import os | 
|---|
| 36 | import xstruct | 
|---|
| 37 |  | 
|---|
| 38 | HEADER = """little: | 
|---|
| 39 | char tag[4]            /* 'HORD' */ | 
|---|
| 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 | """ | 
|---|
| 45 |  | 
|---|
| 46 | HORD_LSB = 1 | 
|---|
| 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) | 
|---|
| 66 | if (align <= 0): | 
|---|
| 67 | print "<ALIGNMENT> must be positive" | 
|---|
| 68 | return | 
|---|
| 69 |  | 
|---|
| 70 | fs_image = os.path.abspath(sys.argv[2]) | 
|---|
| 71 | if (not os.path.isfile(fs_image)): | 
|---|
| 72 | print "<FS_IMAGE> must be a file" | 
|---|
| 73 | return | 
|---|
| 74 |  | 
|---|
| 75 | inf = file(fs_image, "rb") | 
|---|
| 76 | outf = file(sys.argv[3], "wb") | 
|---|
| 77 |  | 
|---|
| 78 | header = xstruct.create(HEADER) | 
|---|
| 79 |  | 
|---|
| 80 | header_size = header.size() | 
|---|
| 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) | 
|---|
| 85 |  | 
|---|
| 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()) | 
|---|
| 94 |  | 
|---|
| 95 | outf.write(inf.read()) | 
|---|
| 96 |  | 
|---|
| 97 | padding = payload_size_aligned - payload_size | 
|---|
| 98 | if (padding > 0): | 
|---|
| 99 | outf.write(xstruct.create("little: padding[%d]" % padding).pack()) | 
|---|
| 100 |  | 
|---|
| 101 | inf.close() | 
|---|
| 102 | outf.close() | 
|---|
| 103 |  | 
|---|
| 104 | if __name__ == '__main__': | 
|---|
| 105 | main() | 
|---|