diff options
Diffstat (limited to 'firmware/export/linuxboot.h')
-rw-r--r-- | firmware/export/linuxboot.h | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/firmware/export/linuxboot.h b/firmware/export/linuxboot.h new file mode 100644 index 0000000000..7dbc213012 --- /dev/null +++ b/firmware/export/linuxboot.h | |||
@@ -0,0 +1,189 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2022 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #ifndef __LINUXBOOT_H__ | ||
23 | #define __LINUXBOOT_H__ | ||
24 | |||
25 | #include "system.h" | ||
26 | #include <stddef.h> | ||
27 | #include <sys/types.h> | ||
28 | |||
29 | /* | ||
30 | * From u-boot's include/image.h | ||
31 | * SPDX-License-Identifier: GPL-2.0+ | ||
32 | * (C) Copyright 2008 Semihalf | ||
33 | * (C) Copyright 2000-2005 | ||
34 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | ||
35 | */ | ||
36 | |||
37 | #define IH_MAGIC 0x27051956 | ||
38 | #define IH_NMLEN 32 | ||
39 | |||
40 | enum | ||
41 | { | ||
42 | IH_ARCH_INVALID, | ||
43 | IH_ARCH_ALPHA, | ||
44 | IH_ARCH_ARM, | ||
45 | IH_ARCH_I386, | ||
46 | IH_ARCH_IA64, | ||
47 | IH_ARCH_MIPS, | ||
48 | IH_ARCH_MIPS64, | ||
49 | IH_ARCH_PPC, | ||
50 | IH_ARCH_S390, | ||
51 | IH_ARCH_SH, | ||
52 | IH_ARCH_SPARC, | ||
53 | IH_ARCH_SPARC64, | ||
54 | IH_ARCH_M68K, | ||
55 | /* NOTE: Other archs not relevant and omitted here, can add if needed */ | ||
56 | IH_ARCH_COUNT, | ||
57 | }; | ||
58 | |||
59 | enum | ||
60 | { | ||
61 | IH_TYPE_INVALID = 0, | ||
62 | IN_TYPE_STANDALONE, | ||
63 | IH_TYPE_KERNEL, | ||
64 | IH_TYPE_RAMDISK, | ||
65 | IH_TYPE_MULTI, | ||
66 | IH_TYPE_FIRMWARE, | ||
67 | IH_TYPE_SCRIPT, | ||
68 | IH_TYPE_FILESYSTEM, | ||
69 | IH_TYPE_FLATDT, | ||
70 | /* NOTE: Other types not relevant and omitted here, can add if needed */ | ||
71 | IH_TYPE_COUNT, | ||
72 | }; | ||
73 | |||
74 | enum | ||
75 | { | ||
76 | IH_COMP_NONE = 0, | ||
77 | IH_COMP_GZIP, | ||
78 | IH_COMP_BZIP2, | ||
79 | IH_COMP_LZMA, | ||
80 | IH_COMP_LZO, | ||
81 | IH_COMP_LZ4, | ||
82 | IH_COMP_ZSTD, | ||
83 | IH_COMP_COUNT, | ||
84 | }; | ||
85 | |||
86 | /* Legacy U-Boot image header as produced by mkimage(1). | ||
87 | * | ||
88 | * WARNING: all fields are big-endian so you usually do not want to | ||
89 | * access them directly, use the accessor functions instead. | ||
90 | */ | ||
91 | struct uimage_header | ||
92 | { | ||
93 | uint32_t ih_magic; | ||
94 | uint32_t ih_hcrc; | ||
95 | uint32_t ih_time; | ||
96 | uint32_t ih_size; | ||
97 | uint32_t ih_load; | ||
98 | uint32_t ih_ep; | ||
99 | uint32_t ih_dcrc; | ||
100 | uint8_t ih_os; | ||
101 | uint8_t ih_arch; | ||
102 | uint8_t ih_type; | ||
103 | uint8_t ih_comp; | ||
104 | uint8_t ih_name[IH_NMLEN]; | ||
105 | }; | ||
106 | |||
107 | #define _uimage_get32_f(name) \ | ||
108 | static inline uint32_t uimage_get_##name(const struct uimage_header* uh) \ | ||
109 | { return betoh32(uh->ih_##name); } | ||
110 | _uimage_get32_f(magic) | ||
111 | _uimage_get32_f(hcrc) | ||
112 | _uimage_get32_f(time) | ||
113 | _uimage_get32_f(size) | ||
114 | _uimage_get32_f(load) | ||
115 | _uimage_get32_f(ep) | ||
116 | _uimage_get32_f(dcrc) | ||
117 | #undef _uimage_get32_f | ||
118 | |||
119 | #define _uimage_set32_f(name) \ | ||
120 | static inline void uimage_set_##name(struct uimage_header* uh, uint32_t val) \ | ||
121 | { uh->ih_##name = htobe32(val); } | ||
122 | _uimage_set32_f(magic) | ||
123 | _uimage_set32_f(hcrc) | ||
124 | _uimage_set32_f(time) | ||
125 | _uimage_set32_f(size) | ||
126 | _uimage_set32_f(load) | ||
127 | _uimage_set32_f(ep) | ||
128 | _uimage_set32_f(dcrc) | ||
129 | #undef _uimage_set32_f | ||
130 | |||
131 | #define _uimage_get8_f(name) \ | ||
132 | static inline uint8_t uimage_get_##name(const struct uimage_header* uh) \ | ||
133 | { return uh->ih_##name; } | ||
134 | _uimage_get8_f(os) | ||
135 | _uimage_get8_f(arch) | ||
136 | _uimage_get8_f(type) | ||
137 | _uimage_get8_f(comp) | ||
138 | #undef _uimage_get8_f | ||
139 | |||
140 | #define _uimage_set8_f(name) \ | ||
141 | static inline void uimage_set_##name(struct uimage_header* uh, uint8_t val) \ | ||
142 | { uh->ih_##name = val; } | ||
143 | _uimage_set8_f(os) | ||
144 | _uimage_set8_f(arch) | ||
145 | _uimage_set8_f(type) | ||
146 | _uimage_set8_f(comp) | ||
147 | #undef _uimage_set8_f | ||
148 | |||
149 | /* | ||
150 | * uImage utilities | ||
151 | */ | ||
152 | |||
153 | /** Reader callback for use with `uimage_load` | ||
154 | * | ||
155 | * \param buf Buffer to write into | ||
156 | * \param size Number of bytes to read | ||
157 | * \param ctx State argument | ||
158 | * \return Number of bytes actually read, or -1 on error. | ||
159 | */ | ||
160 | typedef ssize_t(*uimage_reader)(void* buf, size_t size, void* ctx); | ||
161 | |||
162 | /** Calculate U-Boot style CRC */ | ||
163 | uint32_t uimage_crc(uint32_t crc, const void* data, size_t size); | ||
164 | |||
165 | /** Calculate CRC of a uImage header */ | ||
166 | uint32_t uimage_calc_hcrc(const struct uimage_header* uh); | ||
167 | |||
168 | /** Load and decompress a uImage | ||
169 | * | ||
170 | * \param uh Returned header struct (will be filled with read data) | ||
171 | * \param out_size Returned size of the decompressed data | ||
172 | * \param reader Data reader function | ||
173 | * \param rctx Context argument for the reader function | ||
174 | * \return Buflib handle containing the decompressed data, or negative on error | ||
175 | * | ||
176 | * This function will read a uImage, verify checksums and decompress the image | ||
177 | * data into a non-moveable buflib allocation. The length of the compressed | ||
178 | * data will be taken from the uImage header. | ||
179 | */ | ||
180 | int uimage_load(struct uimage_header* uh, size_t* out_size, | ||
181 | uimage_reader reader, void* rctx); | ||
182 | |||
183 | /** File reader for use with `uimage_load` | ||
184 | * | ||
185 | * \param ctx File descriptor, casted to `void*` | ||
186 | */ | ||
187 | ssize_t uimage_fd_reader(void* buf, size_t size, void* ctx); | ||
188 | |||
189 | #endif /* __LINUXBOOT_H__ */ | ||