.. | .. |
---|
4 | 4 | * SPDX-License-Identifier: GPL-2.0+ |
---|
5 | 5 | */ |
---|
6 | 6 | #include <common.h> |
---|
7 | | -#include <adc.h> |
---|
8 | | -#include <android_ab.h> |
---|
9 | | -#include <android_bootloader.h> |
---|
10 | | -#include <android_image.h> |
---|
11 | 7 | #include <boot_rkimg.h> |
---|
12 | 8 | #include <bmp_layout.h> |
---|
13 | 9 | #include <malloc.h> |
---|
14 | | -#include <asm/io.h> |
---|
15 | 10 | #include <asm/unaligned.h> |
---|
16 | | -#include <dm/ofnode.h> |
---|
| 11 | +#include <linux/libfdt.h> |
---|
17 | 12 | #include <linux/list.h> |
---|
18 | | -#include <asm/arch/fit.h> |
---|
19 | | -#include <asm/arch/uimage.h> |
---|
20 | 13 | #include <asm/arch/resource_img.h> |
---|
| 14 | +#include <asm/arch/uimage.h> |
---|
| 15 | +#include <asm/arch/fit.h> |
---|
21 | 16 | |
---|
22 | 17 | DECLARE_GLOBAL_DATA_PTR; |
---|
23 | 18 | |
---|
24 | 19 | #define PART_RESOURCE "resource" |
---|
25 | 20 | #define RESOURCE_MAGIC "RSCE" |
---|
26 | 21 | #define RESOURCE_MAGIC_SIZE 4 |
---|
27 | | -#define RESOURCE_VERSION 0 |
---|
28 | | -#define CONTENT_VERSION 0 |
---|
29 | 22 | #define ENTRY_TAG "ENTR" |
---|
30 | 23 | #define ENTRY_TAG_SIZE 4 |
---|
31 | 24 | #define MAX_FILE_NAME_LEN 220 |
---|
.. | .. |
---|
70 | 63 | */ |
---|
71 | 64 | |
---|
72 | 65 | /** |
---|
73 | | - * struct resource_image_header |
---|
| 66 | + * struct resource_img_hdr |
---|
74 | 67 | * |
---|
75 | 68 | * @magic: should be "RSCE" |
---|
76 | 69 | * @version: resource image version, current is 0 |
---|
.. | .. |
---|
80 | 73 | * @e_blks: the size(by block) of the entry in the contents |
---|
81 | 74 | * @e_num: numbers of the entrys. |
---|
82 | 75 | */ |
---|
83 | | - |
---|
84 | 76 | struct resource_img_hdr { |
---|
85 | 77 | char magic[4]; |
---|
86 | 78 | uint16_t version; |
---|
.. | .. |
---|
96 | 88 | char name[MAX_FILE_NAME_LEN]; |
---|
97 | 89 | char hash[MAX_HASH_LEN]; |
---|
98 | 90 | uint32_t hash_size; |
---|
99 | | - uint32_t f_offset; /* Sector offset */ |
---|
100 | | - uint32_t f_size; /* Bytes */ |
---|
| 91 | + uint32_t blk_offset; |
---|
| 92 | + uint32_t size; /* in byte */ |
---|
101 | 93 | }; |
---|
102 | 94 | |
---|
103 | | -LIST_HEAD(entrys_head); |
---|
104 | | -LIST_HEAD(entrys_dtbs_head); |
---|
| 95 | +LIST_HEAD(entry_head); |
---|
105 | 96 | |
---|
106 | | -__weak int board_resource_dtb_accepted(char *dtb_name) |
---|
| 97 | +static int resource_check_header(struct resource_img_hdr *hdr) |
---|
107 | 98 | { |
---|
108 | | - return 1; |
---|
| 99 | + return memcmp(RESOURCE_MAGIC, hdr->magic, RESOURCE_MAGIC_SIZE); |
---|
109 | 100 | } |
---|
110 | 101 | |
---|
111 | | -int resource_image_check_header(void *rsce_hdr) |
---|
| 102 | +static void resource_dump(struct resource_file *f) |
---|
112 | 103 | { |
---|
113 | | - struct resource_img_hdr *hdr = rsce_hdr; |
---|
114 | | - int ret; |
---|
115 | | - |
---|
116 | | - ret = memcmp(RESOURCE_MAGIC, hdr->magic, RESOURCE_MAGIC_SIZE); |
---|
117 | | - if (ret) { |
---|
118 | | - debug("bad resource image magic: %s\n", |
---|
119 | | - hdr->magic ? hdr->magic : "none"); |
---|
120 | | - ret = -EINVAL; |
---|
121 | | - } |
---|
122 | | - |
---|
123 | | - debug("resource image header:\n"); |
---|
124 | | - debug("magic:%s\n", hdr->magic); |
---|
125 | | - debug("version:%d\n", hdr->version); |
---|
126 | | - debug("c_version:%d\n", hdr->c_version); |
---|
127 | | - debug("blks:%d\n", hdr->blks); |
---|
128 | | - debug("c_offset:%d\n", hdr->c_offset); |
---|
129 | | - debug("e_blks:%d\n", hdr->e_blks); |
---|
130 | | - debug("e_num:%d\n", hdr->e_nums); |
---|
131 | | - |
---|
132 | | - return ret; |
---|
| 104 | + printf("%s\n", f->name); |
---|
| 105 | + printf(" blk_start: 0x%08lx\n", (ulong)f->blk_start); |
---|
| 106 | + printf(" blk_offset: 0x%08lx\n", (ulong)f->blk_offset); |
---|
| 107 | + printf(" size: 0x%08x\n", f->size); |
---|
| 108 | + printf(" in_ram: %d\n", f->in_ram); |
---|
| 109 | + printf(" hash_size: %d\n\n", f->hash_size); |
---|
133 | 110 | } |
---|
134 | 111 | |
---|
135 | | -static int add_file_to_list(struct resource_entry *entry, int rsce_base, bool ram) |
---|
| 112 | +static int resource_add_file(const char *name, u32 size, |
---|
| 113 | + u32 blk_start, u32 blk_offset, |
---|
| 114 | + char *hash, u32 hash_size, |
---|
| 115 | + bool in_ram) |
---|
136 | 116 | { |
---|
137 | | - struct resource_file *file; |
---|
138 | | - |
---|
139 | | - if (memcmp(entry->tag, ENTRY_TAG, ENTRY_TAG_SIZE)) { |
---|
140 | | - debug("invalid entry tag\n"); |
---|
141 | | - return -ENOENT; |
---|
142 | | - } |
---|
143 | | - |
---|
144 | | - file = malloc(sizeof(*file)); |
---|
145 | | - if (!file) { |
---|
146 | | - debug("out of memory\n"); |
---|
147 | | - return -ENOMEM; |
---|
148 | | - } |
---|
149 | | - |
---|
150 | | - strcpy(file->name, entry->name); |
---|
151 | | - file->rsce_base = rsce_base; |
---|
152 | | - file->f_offset = entry->f_offset; |
---|
153 | | - file->f_size = entry->f_size; |
---|
154 | | - file->hash_size = entry->hash_size; |
---|
155 | | - file->ram = ram; |
---|
156 | | - memcpy(file->hash, entry->hash, entry->hash_size); |
---|
157 | | - INIT_LIST_HEAD(&file->dtbs); |
---|
158 | | - list_add_tail(&file->link, &entrys_head); |
---|
159 | | - if (strstr(file->name, DTB_SUFFIX) && board_resource_dtb_accepted(file->name)) |
---|
160 | | - list_add_tail(&file->dtbs, &entrys_dtbs_head); |
---|
161 | | - debug("ENTRY: addr: %p, name: %18s, base: 0x%08x, offset: 0x%08x, size: 0x%08x\n", |
---|
162 | | - entry, file->name, file->rsce_base, file->f_offset, file->f_size); |
---|
163 | | - |
---|
164 | | - return 0; |
---|
165 | | -} |
---|
166 | | - |
---|
167 | | -int resource_replace_entry(const char *f_name, uint32_t base, |
---|
168 | | - uint32_t f_offset, uint32_t f_size) |
---|
169 | | -{ |
---|
170 | | - struct resource_entry *entry; |
---|
171 | | - struct resource_file *file; |
---|
| 117 | + struct resource_file *f; |
---|
172 | 118 | struct list_head *node; |
---|
| 119 | + bool _new = true; |
---|
173 | 120 | |
---|
174 | | - if (!f_name || !f_size) |
---|
175 | | - return -EINVAL; |
---|
176 | | - |
---|
177 | | - entry = calloc(1, sizeof(*entry)); |
---|
178 | | - if (!entry) |
---|
179 | | - return -ENOMEM; |
---|
180 | | - |
---|
181 | | - strcpy(entry->tag, ENTRY_TAG); |
---|
182 | | - strcpy(entry->name, f_name); |
---|
183 | | - entry->f_offset = f_offset; |
---|
184 | | - entry->f_size = f_size; |
---|
185 | | - entry->hash_size = 0; |
---|
186 | | - |
---|
187 | | - /* Delete exist entry, then add this new */ |
---|
188 | | - list_for_each(node, &entrys_head) { |
---|
189 | | - file = list_entry(node, struct resource_file, link); |
---|
190 | | - if (!strcmp(file->name, entry->name)) { |
---|
191 | | - list_del(&file->link); |
---|
192 | | - list_del(&file->dtbs); |
---|
193 | | - free(file); |
---|
| 121 | + /* old one ? */ |
---|
| 122 | + list_for_each(node, &entry_head) { |
---|
| 123 | + f = list_entry(node, struct resource_file, link); |
---|
| 124 | + if (!strcmp(f->name, name)) { |
---|
| 125 | + _new = false; |
---|
194 | 126 | break; |
---|
195 | 127 | } |
---|
196 | 128 | } |
---|
197 | 129 | |
---|
198 | | - add_file_to_list(entry, base, false); |
---|
199 | | - free(entry); |
---|
| 130 | + if (_new) { |
---|
| 131 | + f = calloc(1, sizeof(*f)); |
---|
| 132 | + if (!f) |
---|
| 133 | + return -ENOMEM; |
---|
| 134 | + |
---|
| 135 | + list_add_tail(&f->link, &entry_head); |
---|
| 136 | + } |
---|
| 137 | + |
---|
| 138 | + strcpy(f->name, name); |
---|
| 139 | + f->size = size; |
---|
| 140 | + f->in_ram = in_ram; |
---|
| 141 | + f->blk_start = blk_start; |
---|
| 142 | + f->blk_offset = blk_offset; |
---|
| 143 | + f->hash_size = hash_size; |
---|
| 144 | + memcpy(f->hash, hash, hash_size); |
---|
| 145 | +#ifdef DEBUG |
---|
| 146 | + resource_dump(f); |
---|
| 147 | +#endif |
---|
| 148 | + return 0; |
---|
| 149 | +} |
---|
| 150 | + |
---|
| 151 | +static int resource_setup_list(struct blk_desc *desc, ulong blk_start, |
---|
| 152 | + void *resc_hdr, bool in_ram) |
---|
| 153 | +{ |
---|
| 154 | + struct resource_img_hdr *hdr = resc_hdr; |
---|
| 155 | + struct resource_entry *et; |
---|
| 156 | + u32 i, stride; |
---|
| 157 | + void *pos; |
---|
| 158 | + |
---|
| 159 | + pos = (void *)hdr + hdr->c_offset * desc->blksz; |
---|
| 160 | + stride = hdr->e_blks * desc->blksz; |
---|
| 161 | + |
---|
| 162 | + for (i = 0; i < hdr->e_nums; i++) { |
---|
| 163 | + et = pos + (i * stride); |
---|
| 164 | + if (memcmp(et->tag, ENTRY_TAG, ENTRY_TAG_SIZE)) |
---|
| 165 | + continue; |
---|
| 166 | + |
---|
| 167 | + resource_add_file(et->name, et->size, |
---|
| 168 | + blk_start, et->blk_offset, |
---|
| 169 | + et->hash, et->hash_size, in_ram); |
---|
| 170 | + } |
---|
200 | 171 | |
---|
201 | 172 | return 0; |
---|
202 | 173 | } |
---|
203 | 174 | |
---|
204 | | -int resource_create_ram_list(struct blk_desc *dev_desc, void *rsce_hdr) |
---|
| 175 | +int resource_setup_ram_list(struct blk_desc *desc, void *hdr) |
---|
205 | 176 | { |
---|
206 | | - struct resource_img_hdr *hdr = rsce_hdr; |
---|
207 | | - struct resource_entry *entry; |
---|
208 | | - int e_num, size; |
---|
209 | | - void *data; |
---|
210 | | - int ret = 0; |
---|
| 177 | + if (!desc) |
---|
| 178 | + return -ENODEV; |
---|
211 | 179 | |
---|
212 | | - if (resource_image_check_header(hdr)) { |
---|
213 | | - ret = -EINVAL; |
---|
214 | | - goto out; |
---|
| 180 | + if (resource_check_header(hdr)) { |
---|
| 181 | + printf("RESC: invalid\n"); |
---|
| 182 | + return -EINVAL; |
---|
215 | 183 | } |
---|
216 | 184 | |
---|
217 | | - list_del_init(&entrys_head); |
---|
218 | | - list_del_init(&entrys_dtbs_head); |
---|
219 | | - data = (void *)((ulong)hdr + hdr->c_offset * dev_desc->blksz); |
---|
220 | | - for (e_num = 0; e_num < hdr->e_nums; e_num++) { |
---|
221 | | - size = e_num * hdr->e_blks * dev_desc->blksz; |
---|
222 | | - entry = (struct resource_entry *)(data + size); |
---|
223 | | - add_file_to_list(entry, (ulong)hdr, true); |
---|
| 185 | + /* @blk_start: set as 'hdr' point addr, to be used in byte */ |
---|
| 186 | + return resource_setup_list(desc, (ulong)hdr, hdr, true); |
---|
| 187 | +} |
---|
| 188 | + |
---|
| 189 | +#ifdef CONFIG_ANDROID_BOOT_IMAGE |
---|
| 190 | +/* |
---|
| 191 | + * Add logo.bmp and logo_kernel.bmp from "logo" parititon |
---|
| 192 | + * |
---|
| 193 | + * Provide a "logo" partition for user to store logo.bmp and |
---|
| 194 | + * logo_kernel.bmp, so that the user can update them from |
---|
| 195 | + * kernel or user-space dynamically. |
---|
| 196 | + * |
---|
| 197 | + * "logo" partition layout, do not change order: |
---|
| 198 | + * |
---|
| 199 | + * |----------------------| 0x00 |
---|
| 200 | + * | raw logo.bmp | |
---|
| 201 | + * |----------------------| -> 512-byte aligned |
---|
| 202 | + * | raw logo_kernel.bmp | |
---|
| 203 | + * |----------------------| |
---|
| 204 | + * |
---|
| 205 | + * N: the sector count of logo.bmp |
---|
| 206 | + * |
---|
| 207 | + * How to generate: |
---|
| 208 | + * cat logo.bmp > logo.img && truncate -s %512 logo.img && cat logo_kernel.bmp >> logo.img |
---|
| 209 | + */ |
---|
| 210 | +static int resource_setup_logo_bmp(struct blk_desc *desc) |
---|
| 211 | +{ |
---|
| 212 | + struct bmp_header *header; |
---|
| 213 | + const char *name[] = { "logo.bmp", "logo_kernel.bmp" }; |
---|
| 214 | + disk_partition_t part; |
---|
| 215 | + u32 blk_offset = 0; |
---|
| 216 | + u32 filesz; |
---|
| 217 | + int ret, i; |
---|
| 218 | + |
---|
| 219 | + if (part_get_info_by_name(desc, PART_LOGO, &part) < 0) |
---|
| 220 | + return 0; |
---|
| 221 | + |
---|
| 222 | + header = memalign(ARCH_DMA_MINALIGN, desc->blksz); |
---|
| 223 | + if (!header) |
---|
| 224 | + return -ENOMEM; |
---|
| 225 | + |
---|
| 226 | + for (i = 0; i < ARRAY_SIZE(name); i++) { |
---|
| 227 | + if (blk_dread(desc, part.start + blk_offset, 1, header) != 1) { |
---|
| 228 | + ret = -EIO; |
---|
| 229 | + break; |
---|
| 230 | + } |
---|
| 231 | + |
---|
| 232 | + if (header->signature[0] != 'B' || header->signature[1] != 'M') { |
---|
| 233 | + ret = -EINVAL; |
---|
| 234 | + break; |
---|
| 235 | + } |
---|
| 236 | + |
---|
| 237 | + filesz = get_unaligned_le32(&header->file_size); |
---|
| 238 | + ret = resource_add_file(name[i], filesz, part.start, blk_offset, |
---|
| 239 | + NULL, 0, false); |
---|
| 240 | + if (ret) |
---|
| 241 | + break; |
---|
| 242 | + |
---|
| 243 | + /* move to next file */ |
---|
| 244 | + blk_offset += DIV_ROUND_UP(filesz, desc->blksz); |
---|
| 245 | + |
---|
| 246 | + printf("LOGO: %s\n", name[i]); |
---|
| 247 | + |
---|
224 | 248 | } |
---|
225 | | -out: |
---|
226 | | - resource_read_logo_bmps(); |
---|
| 249 | + |
---|
| 250 | + free(header); |
---|
227 | 251 | |
---|
228 | 252 | return ret; |
---|
229 | 253 | } |
---|
230 | 254 | |
---|
231 | | -static int resource_create_list(struct blk_desc *dev_desc, int rsce_base) |
---|
| 255 | +static int resource_setup_blk_list(struct blk_desc *desc, ulong blk_start) |
---|
232 | 256 | { |
---|
233 | 257 | struct resource_img_hdr *hdr; |
---|
234 | | - struct resource_entry *entry; |
---|
235 | | - int blknum, e_num; |
---|
236 | | - void *data = NULL; |
---|
| 258 | + int blk_cnt; |
---|
237 | 259 | int ret = 0; |
---|
238 | | - int size; |
---|
| 260 | + void *buf; |
---|
239 | 261 | |
---|
240 | | - hdr = memalign(ARCH_DMA_MINALIGN, dev_desc->blksz); |
---|
| 262 | + hdr = memalign(ARCH_DMA_MINALIGN, desc->blksz); |
---|
241 | 263 | if (!hdr) |
---|
242 | 264 | return -ENOMEM; |
---|
243 | 265 | |
---|
244 | | - if (blk_dread(dev_desc, rsce_base, 1, hdr) != 1) { |
---|
245 | | - printf("Failed to read resource hdr\n"); |
---|
| 266 | + if (blk_dread(desc, blk_start, 1, hdr) != 1) { |
---|
246 | 267 | ret = -EIO; |
---|
247 | | - goto err; |
---|
| 268 | + goto out; |
---|
248 | 269 | } |
---|
249 | 270 | |
---|
250 | | - if (resource_image_check_header(hdr)) { |
---|
| 271 | + if (resource_check_header(hdr)) { |
---|
| 272 | + printf("RESC: invalid\n"); |
---|
251 | 273 | if (fdt_check_header(hdr)) { |
---|
252 | | - printf("No valid resource or dtb file\n"); |
---|
253 | 274 | ret = -EINVAL; |
---|
254 | | - goto err; |
---|
| 275 | + goto out; |
---|
255 | 276 | } else { |
---|
256 | | - free(hdr); |
---|
257 | | - return resource_replace_entry(DEFAULT_DTB_FILE, rsce_base, |
---|
258 | | - 0, fdt_totalsize(hdr)); |
---|
| 277 | + /* this is a dtb file */ |
---|
| 278 | + printf("RESC: this is dtb\n"); |
---|
| 279 | + ret = resource_add_file(DEFAULT_DTB_FILE, |
---|
| 280 | + fdt_totalsize(hdr), |
---|
| 281 | + blk_start, 0, NULL, 0, false); |
---|
| 282 | + goto out; |
---|
259 | 283 | } |
---|
260 | 284 | } |
---|
261 | 285 | |
---|
262 | | - blknum = hdr->e_blks * hdr->e_nums; |
---|
263 | | - data = memalign(ARCH_DMA_MINALIGN, blknum * dev_desc->blksz); |
---|
264 | | - if (!data) { |
---|
| 286 | + blk_cnt = hdr->e_blks * hdr->e_nums; |
---|
| 287 | + hdr = realloc(hdr, (1 + blk_cnt) * desc->blksz); |
---|
| 288 | + if (!hdr) { |
---|
265 | 289 | ret = -ENOMEM; |
---|
266 | | - goto err; |
---|
| 290 | + goto out; |
---|
267 | 291 | } |
---|
268 | 292 | |
---|
269 | | - if (blk_dread(dev_desc, rsce_base + hdr->c_offset, |
---|
270 | | - blknum, data) != blknum) { |
---|
271 | | - printf("Failed to read resource entries\n"); |
---|
| 293 | + buf = (void *)hdr + desc->blksz; |
---|
| 294 | + if (blk_dread(desc, blk_start + hdr->c_offset, blk_cnt, buf) != blk_cnt) { |
---|
272 | 295 | ret = -EIO; |
---|
273 | | - goto err; |
---|
| 296 | + goto out; |
---|
274 | 297 | } |
---|
275 | 298 | |
---|
276 | | - /* |
---|
277 | | - * Add all file into resource file list, and load what we want from |
---|
278 | | - * storage when we really need it. |
---|
279 | | - */ |
---|
280 | | - for (e_num = 0; e_num < hdr->e_nums; e_num++) { |
---|
281 | | - size = e_num * hdr->e_blks * dev_desc->blksz; |
---|
282 | | - entry = (struct resource_entry *)(data + size); |
---|
283 | | - add_file_to_list(entry, rsce_base, false); |
---|
284 | | - } |
---|
285 | | - |
---|
286 | | -err: |
---|
287 | | - if (data) |
---|
288 | | - free(data); |
---|
289 | | - if (hdr) |
---|
290 | | - free(hdr); |
---|
291 | | - |
---|
292 | | - resource_read_logo_bmps(); |
---|
| 299 | + resource_setup_list(desc, blk_start, hdr, false); |
---|
| 300 | + resource_setup_logo_bmp(desc); |
---|
| 301 | +out: |
---|
| 302 | + free(hdr); |
---|
293 | 303 | |
---|
294 | 304 | return ret; |
---|
295 | 305 | } |
---|
296 | 306 | |
---|
297 | | -static int read_dtb_from_android(struct blk_desc *dev_desc, |
---|
298 | | - struct andr_img_hdr *hdr, |
---|
299 | | - ulong rsce_base) |
---|
| 307 | +static int resource_init(struct blk_desc *desc, |
---|
| 308 | + disk_partition_t *part, |
---|
| 309 | + ulong blk_offset) |
---|
300 | 310 | { |
---|
301 | | - ulong dtb_offset = 0; |
---|
302 | | - ulong dtb_size = 0; |
---|
| 311 | + printf("RESC: '%s', blk@0x%08lx\n", part->name, part->start + blk_offset); |
---|
303 | 312 | |
---|
304 | | - if (!hdr || hdr->header_version <= 1) { |
---|
305 | | - return 0; |
---|
306 | | - } else if (hdr->header_version == 2) { |
---|
307 | | - dtb_offset += hdr->page_size; |
---|
308 | | - dtb_offset += ALIGN(hdr->kernel_size, hdr->page_size); |
---|
309 | | - dtb_offset += ALIGN(hdr->ramdisk_size, hdr->page_size); |
---|
310 | | - dtb_offset += ALIGN(hdr->recovery_dtbo_size, hdr->page_size) + |
---|
311 | | - ALIGN(hdr->second_size, hdr->page_size); |
---|
312 | | - dtb_size = hdr->dtb_size; |
---|
313 | | - } else if (hdr->header_version >= 3) { |
---|
314 | | - ulong vendor_boot_hdr_size = (hdr->header_version == 3) ? |
---|
315 | | - VENDOR_BOOT_HDRv3_SIZE : VENDOR_BOOT_HDRv4_SIZE; |
---|
| 313 | +#ifdef CONFIG_ANDROID_AVB |
---|
| 314 | + char hdr[512]; |
---|
| 315 | + ulong resc_buf = 0; |
---|
| 316 | + int ret; |
---|
316 | 317 | |
---|
317 | | - dtb_offset += ALIGN(vendor_boot_hdr_size, |
---|
318 | | - hdr->vendor_page_size) + |
---|
319 | | - ALIGN(hdr->vendor_ramdisk_size, |
---|
320 | | - hdr->vendor_page_size); |
---|
321 | | - dtb_size = hdr->dtb_size; |
---|
| 318 | + if (blk_dread(desc, part->start, 1, hdr) != 1) |
---|
| 319 | + return -EIO; |
---|
| 320 | + |
---|
| 321 | + /* only handle android boot/recovery.img and resource.img, ignore fit */ |
---|
| 322 | + if (!android_image_check_header((void *)hdr) || |
---|
| 323 | + !resource_check_header((void *)hdr)) { |
---|
| 324 | + ret = android_image_verify_resource((const char *)part->name, &resc_buf); |
---|
| 325 | + if (ret) { |
---|
| 326 | + printf("RESC: '%s', avb verify fail: %d\n", part->name, ret); |
---|
| 327 | + return ret; |
---|
| 328 | + } |
---|
| 329 | + |
---|
| 330 | + /* |
---|
| 331 | + * unlock=0: resc_buf is valid and file was already full load in ram. |
---|
| 332 | + * unlock=1: resc_buf is 0. |
---|
| 333 | + */ |
---|
| 334 | + if (resc_buf && !resource_check_header((void *)resc_buf)) |
---|
| 335 | + return resource_setup_ram_list(desc, (void *)resc_buf); |
---|
322 | 336 | } |
---|
| 337 | +#endif |
---|
323 | 338 | |
---|
324 | | - if (!dtb_size) |
---|
325 | | - return 0; |
---|
| 339 | + return resource_setup_blk_list(desc, part->start + blk_offset); |
---|
| 340 | +} |
---|
326 | 341 | |
---|
327 | | - /* |
---|
328 | | - * boot_img_hdr_v234 feature. |
---|
329 | | - * |
---|
330 | | - * If dtb position is present, replace the old with new one if |
---|
331 | | - * we don't need to verify DTB hash from resource.img file entry. |
---|
332 | | - */ |
---|
333 | | - dtb_offset = DIV_ROUND_UP(dtb_offset, dev_desc->blksz); |
---|
334 | | - env_update("bootargs", "androidboot.dtb_idx=0"); |
---|
| 342 | +static int resource_default(struct blk_desc *desc, |
---|
| 343 | + disk_partition_t *out_part, |
---|
| 344 | + ulong *out_blk_offset) |
---|
| 345 | +{ |
---|
| 346 | + disk_partition_t part; |
---|
| 347 | + |
---|
| 348 | + if (part_get_info_by_name(desc, PART_RESOURCE, &part) < 0) |
---|
| 349 | + return -ENODEV; |
---|
| 350 | + |
---|
| 351 | + *out_part = part; |
---|
| 352 | + *out_blk_offset = 0; |
---|
335 | 353 | |
---|
336 | 354 | return 0; |
---|
337 | 355 | } |
---|
| 356 | +#endif |
---|
338 | 357 | |
---|
339 | | -static int get_resource_base_sector(struct blk_desc *dev_desc, |
---|
340 | | - struct andr_img_hdr **ret_hdr) |
---|
| 358 | +static int resource_scan(void) |
---|
341 | 359 | { |
---|
| 360 | + struct blk_desc *desc = rockchip_get_bootdev(); |
---|
| 361 | + __maybe_unused int ret; |
---|
| 362 | + |
---|
| 363 | + if (!desc) { |
---|
| 364 | + printf("RESC: No bootdev\n"); |
---|
| 365 | + return -ENODEV; |
---|
| 366 | + } |
---|
| 367 | + |
---|
| 368 | + if (!list_empty(&entry_head)) |
---|
| 369 | + return 0; |
---|
| 370 | + |
---|
| 371 | +#ifdef CONFIG_ROCKCHIP_FIT_IMAGE |
---|
| 372 | + ret = fit_image_init_resource(desc); |
---|
| 373 | + if (!ret || ret != -EAGAIN) |
---|
| 374 | + return ret; |
---|
| 375 | +#endif |
---|
| 376 | +#ifdef CONFIG_ROCKCHIP_UIMAGE |
---|
| 377 | + ret = uimage_init_resource(desc); |
---|
| 378 | + if (!ret || ret != -EAGAIN) |
---|
| 379 | + return ret; |
---|
| 380 | +#endif |
---|
| 381 | +#ifdef CONFIG_ANDROID_BOOT_IMAGE |
---|
342 | 382 | disk_partition_t part; |
---|
343 | | - int rsce_base = 0; |
---|
344 | | -#ifdef CONFIG_ANDROID_BOOT_IMAGE |
---|
345 | | - struct andr_img_hdr *hdr; |
---|
346 | | - u32 os_ver = 0, os_lvl; |
---|
347 | | - const char *part_boot = PART_BOOT; |
---|
| 383 | + ulong blk_offset; |
---|
| 384 | + char hdr[512]; |
---|
| 385 | + char name[32]; |
---|
348 | 386 | |
---|
349 | | - /* |
---|
350 | | - * Anyway, we must read android hdr firstly from boot/recovery partition |
---|
351 | | - * to get the 'os_version' for android_bcb_msg_sector_offset(), in order |
---|
352 | | - * to confirm BCB message offset of *MISC* partition. |
---|
353 | | - */ |
---|
354 | | -#ifdef CONFIG_ANDROID_AB |
---|
355 | | - part_boot = ab_can_find_recovery_part() ? PART_RECOVERY : PART_BOOT; |
---|
356 | | -#endif |
---|
| 387 | + /* partition priority: boot/recovery > resource */ |
---|
| 388 | + if (!android_image_init_resource(desc, &part, &blk_offset)) { |
---|
| 389 | + if (blk_dread(desc, part.start + blk_offset, 1, hdr) != 1) |
---|
| 390 | + return -EIO; |
---|
357 | 391 | |
---|
358 | | - if (part_get_info_by_name(dev_desc, part_boot, &part) < 0) |
---|
359 | | - goto resource_part; |
---|
| 392 | + if (resource_check_header((void *)hdr)) { |
---|
| 393 | + strcpy(name, (char *)part.name); |
---|
| 394 | + if (resource_default(desc, &part, &blk_offset)) |
---|
| 395 | + return -ENOENT; |
---|
360 | 396 | |
---|
361 | | - hdr = populate_andr_img_hdr(dev_desc, &part); |
---|
362 | | - if (hdr) { |
---|
363 | | - os_ver = hdr->os_version >> 11; |
---|
364 | | - os_lvl = hdr->os_version & ((1U << 11) - 1); |
---|
365 | | - if (os_ver) |
---|
366 | | - gd->bd->bi_andr_version = hdr->os_version; |
---|
367 | | - } |
---|
368 | | - |
---|
369 | | -#ifndef CONFIG_ANDROID_AB |
---|
370 | | - /* Get boot mode from misc and read if recovery mode */ |
---|
371 | | - if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) { |
---|
372 | | - if (hdr) |
---|
373 | | - free(hdr); |
---|
374 | | - |
---|
375 | | - if (part_get_info_by_name(dev_desc, PART_RECOVERY, &part) < 0) |
---|
376 | | - goto resource_part; |
---|
377 | | - |
---|
378 | | - hdr = populate_andr_img_hdr(dev_desc, &part); |
---|
379 | | - if (!hdr) |
---|
380 | | - goto resource_part; |
---|
381 | | - } |
---|
382 | | -#endif |
---|
383 | | - /* If Android v012, getting resource from second position ! */ |
---|
384 | | - if (hdr) { |
---|
385 | | - if (os_ver) |
---|
386 | | - printf("Android %u.%u, Build %u.%u, v%d\n", |
---|
387 | | - (os_ver >> 14) & 0x7F, (os_ver >> 7) & 0x7F, |
---|
388 | | - (os_lvl >> 4) + 2000, os_lvl & 0x0F, |
---|
389 | | - hdr->header_version); |
---|
390 | | - *ret_hdr = hdr; |
---|
391 | | - if (hdr->header_version < 3) { |
---|
392 | | - rsce_base = part.start * dev_desc->blksz; |
---|
393 | | - rsce_base += hdr->page_size; |
---|
394 | | - rsce_base += ALIGN(hdr->kernel_size, hdr->page_size); |
---|
395 | | - rsce_base += ALIGN(hdr->ramdisk_size, hdr->page_size); |
---|
396 | | - rsce_base = DIV_ROUND_UP(rsce_base, dev_desc->blksz); |
---|
397 | | - goto finish; |
---|
| 397 | + printf("RESC: '%s' -> '%s'\n", name, part.name); |
---|
398 | 398 | } |
---|
399 | | - } |
---|
400 | | -resource_part: |
---|
401 | | -#endif |
---|
402 | | - /* resource partition */ |
---|
403 | | - if (part_get_info_by_name(dev_desc, PART_RESOURCE, &part) < 0) { |
---|
404 | | - printf("No resource partition\n"); |
---|
405 | | - return -ENODEV; |
---|
406 | 399 | } else { |
---|
407 | | - rsce_base = part.start; |
---|
| 400 | + if (resource_default(desc, &part, &blk_offset)) |
---|
| 401 | + return -ENOENT; |
---|
408 | 402 | } |
---|
409 | | -#ifdef CONFIG_ANDROID_BOOT_IMAGE |
---|
410 | | -finish: |
---|
| 403 | + |
---|
| 404 | + /* now, 'part' can be boot/recovery/resource */ |
---|
| 405 | + return resource_init(desc, &part, blk_offset); |
---|
411 | 406 | #endif |
---|
412 | | - printf("Found DTB in %s part\n", part.name); |
---|
413 | | - |
---|
414 | | - return rsce_base; |
---|
| 407 | + return -ENOENT; |
---|
415 | 408 | } |
---|
416 | 409 | |
---|
417 | | -/* |
---|
418 | | - * There are: logo/battery pictures and dtb file in the resource image by default. |
---|
419 | | - * |
---|
420 | | - * This function does: |
---|
421 | | - * |
---|
422 | | - * 1. Get resource image base sector from: boot/recovery(AOSP) > resource(RK) |
---|
423 | | - * 2. Create resource files list(addition: add logo bmps) |
---|
424 | | - * 3. Add dtb from android v2 dtb pos, override the old one from resource file |
---|
425 | | - */ |
---|
426 | | -int resource_init_list(void) |
---|
| 410 | +static struct resource_file *resource_get_file(const char *name) |
---|
427 | 411 | { |
---|
428 | | - struct andr_img_hdr *hdr = NULL; |
---|
429 | | - struct blk_desc *dev_desc; |
---|
430 | | - int rsce_base; |
---|
431 | | - |
---|
432 | | - dev_desc = rockchip_get_bootdev(); |
---|
433 | | - if (!dev_desc) { |
---|
434 | | - printf("No dev_desc!\n"); |
---|
435 | | - return -ENODEV; |
---|
436 | | - } |
---|
437 | | - |
---|
438 | | - rsce_base = get_resource_base_sector(dev_desc, &hdr); |
---|
439 | | - if (rsce_base > 0) { |
---|
440 | | - if (resource_create_list(dev_desc, rsce_base)) |
---|
441 | | - printf("Failed to create resource list\n"); |
---|
442 | | - } |
---|
443 | | - |
---|
444 | | - /* override the resource dtb with android dtb if need */ |
---|
445 | | - return read_dtb_from_android(dev_desc, hdr, rsce_base); |
---|
446 | | -} |
---|
447 | | - |
---|
448 | | -int resource_is_empty(void) |
---|
449 | | -{ |
---|
450 | | - return list_empty(&entrys_head); |
---|
451 | | -} |
---|
452 | | - |
---|
453 | | -static struct resource_file *get_file_info(const char *name) |
---|
454 | | -{ |
---|
455 | | - struct resource_file *file; |
---|
| 412 | + struct resource_file *f; |
---|
456 | 413 | struct list_head *node; |
---|
457 | 414 | |
---|
458 | | - if (list_empty(&entrys_head)) { |
---|
459 | | - if (resource_init_list()) |
---|
460 | | - return NULL; |
---|
461 | | - } |
---|
| 415 | + if (resource_scan()) |
---|
| 416 | + return NULL; |
---|
462 | 417 | |
---|
463 | | - list_for_each(node, &entrys_head) { |
---|
464 | | - file = list_entry(node, struct resource_file, link); |
---|
465 | | - if (!strcmp(file->name, name)) |
---|
466 | | - return file; |
---|
| 418 | + list_for_each(node, &entry_head) { |
---|
| 419 | + f = list_entry(node, struct resource_file, link); |
---|
| 420 | + if (!strcmp(f->name, name)) |
---|
| 421 | + return f; |
---|
467 | 422 | } |
---|
468 | 423 | |
---|
469 | 424 | return NULL; |
---|
470 | 425 | } |
---|
471 | 426 | |
---|
472 | | -/* |
---|
473 | | - * read file from resource partition |
---|
474 | | - * @buf: destination buf to store file data; |
---|
475 | | - * @name: file name |
---|
476 | | - * @offset: blocks offset in the file, 1 block = 512 bytes |
---|
477 | | - * @len: the size(by bytes) of file to read. |
---|
478 | | - */ |
---|
479 | | -int rockchip_read_resource_file(void *buf, const char *name, int offset, int len) |
---|
| 427 | +int rockchip_read_resource_file(void *buf, const char *name, int blk_offset, int len) |
---|
480 | 428 | { |
---|
481 | | - struct resource_file *file; |
---|
482 | | - struct blk_desc *dev_desc; |
---|
483 | | - int ret = 0; |
---|
484 | | - int blks; |
---|
485 | | - ulong src; |
---|
| 429 | + struct blk_desc *desc = rockchip_get_bootdev(); |
---|
| 430 | + struct resource_file *f; |
---|
| 431 | + int blk_cnt; |
---|
| 432 | + ulong pos; |
---|
486 | 433 | |
---|
487 | | - file = get_file_info(name); |
---|
488 | | - if (!file) { |
---|
489 | | - printf("No file: %s\n", name); |
---|
| 434 | + if (!desc) |
---|
| 435 | + return -ENODEV; |
---|
| 436 | + |
---|
| 437 | + f = resource_get_file(name); |
---|
| 438 | + if (!f) { |
---|
| 439 | + printf("No resource file: %s\n", name); |
---|
490 | 440 | return -ENOENT; |
---|
491 | 441 | } |
---|
492 | 442 | |
---|
493 | | - dev_desc = rockchip_get_bootdev(); |
---|
494 | | - if (!dev_desc) { |
---|
495 | | - printf("No dev_desc!\n"); |
---|
496 | | - return -ENODEV; |
---|
497 | | - } |
---|
| 443 | + if (len <= 0 || len > f->size) |
---|
| 444 | + len = f->size; |
---|
498 | 445 | |
---|
499 | | - if (len <= 0 || len > file->f_size) |
---|
500 | | - len = file->f_size; |
---|
501 | | - |
---|
502 | | - if (file->ram) { |
---|
503 | | - src = file->rsce_base + |
---|
504 | | - (file->f_offset + offset) * dev_desc->blksz; |
---|
505 | | - memcpy(buf, (char *)src, len); |
---|
506 | | - ret = len; |
---|
| 446 | + if (f->in_ram) { |
---|
| 447 | + pos = f->blk_start + (f->blk_offset + blk_offset) * desc->blksz; |
---|
| 448 | + memcpy(buf, (char *)pos, len); |
---|
507 | 449 | } else { |
---|
508 | | - blks = DIV_ROUND_UP(len, dev_desc->blksz); |
---|
509 | | - ret = blk_dread(dev_desc, |
---|
510 | | - file->rsce_base + file->f_offset + offset, |
---|
511 | | - blks, buf); |
---|
512 | | - ret = (ret != blks) ? -EIO : len; |
---|
| 450 | + blk_cnt = DIV_ROUND_UP(len, desc->blksz); |
---|
| 451 | + if (blk_dread(desc, |
---|
| 452 | + f->blk_start + f->blk_offset + blk_offset, |
---|
| 453 | + blk_cnt, buf) != blk_cnt) |
---|
| 454 | + len = -EIO; |
---|
513 | 455 | } |
---|
514 | 456 | |
---|
515 | | - return ret; |
---|
| 457 | + return len; |
---|
516 | 458 | } |
---|
517 | 459 | |
---|
518 | | -static struct resource_file *get_default_dtb(void) |
---|
519 | | -{ |
---|
520 | | - struct resource_file *target_file = NULL; |
---|
521 | | - struct resource_file *file; |
---|
522 | | - struct list_head *node; |
---|
523 | | - int num = 0; |
---|
524 | | - |
---|
525 | | - if (list_empty(&entrys_head)) { |
---|
526 | | - if (resource_init_list()) |
---|
527 | | - return NULL; |
---|
528 | | - } |
---|
529 | | - |
---|
530 | | - list_for_each(node, &entrys_dtbs_head) { |
---|
531 | | - num++; |
---|
532 | | - file = list_entry(node, struct resource_file, dtbs); |
---|
533 | | - if (strcmp(file->name, DEFAULT_DTB_FILE)) |
---|
534 | | - target_file = file; |
---|
535 | | - } |
---|
536 | | - |
---|
537 | | - /* |
---|
538 | | - * two possible case: |
---|
539 | | - * case 1. rk-kernel.dtb only |
---|
540 | | - * case 2. targe_file(s) + rk-kernel.dtb(maybe they are the same), |
---|
541 | | - * use (last)target_file as result one. |
---|
542 | | - */ |
---|
543 | | - if (num > 2) |
---|
544 | | - printf("Error: find duplicate(%d) dtbs\n", num); |
---|
545 | | - |
---|
546 | | - return target_file ? : get_file_info(DEFAULT_DTB_FILE); |
---|
547 | | -} |
---|
| 460 | +extern struct resource_file *resource_read_hwid_dtb(void); |
---|
548 | 461 | |
---|
549 | 462 | int rockchip_read_resource_dtb(void *fdt_addr, char **hash, int *hash_size) |
---|
550 | 463 | { |
---|
551 | | - struct resource_file *file = NULL; |
---|
| 464 | + struct resource_file *f = NULL; |
---|
552 | 465 | int ret; |
---|
553 | 466 | |
---|
554 | 467 | #ifdef CONFIG_ROCKCHIP_HWID_DTB |
---|
555 | | - file = resource_read_hwid_dtb(); |
---|
556 | | -#endif |
---|
557 | | - /* If no dtb matches hardware id(GPIO/ADC), use the default */ |
---|
558 | | - if (!file) |
---|
559 | | - file = get_default_dtb(); |
---|
| 468 | + if (resource_scan()) |
---|
| 469 | + return -ENOENT; |
---|
560 | 470 | |
---|
561 | | - if (!file) |
---|
| 471 | + f = resource_read_hwid_dtb(); |
---|
| 472 | +#endif |
---|
| 473 | + /* If no dtb match hardware id(GPIO/ADC), use the default */ |
---|
| 474 | + if (!f) |
---|
| 475 | + f = resource_get_file(DEFAULT_DTB_FILE); |
---|
| 476 | + |
---|
| 477 | + if (!f) |
---|
562 | 478 | return -ENODEV; |
---|
563 | 479 | |
---|
564 | | - ret = rockchip_read_resource_file(fdt_addr, file->name, 0, 0); |
---|
| 480 | + ret = rockchip_read_resource_file(fdt_addr, f->name, 0, 0); |
---|
565 | 481 | if (ret < 0) |
---|
566 | 482 | return ret; |
---|
567 | 483 | |
---|
568 | 484 | if (fdt_check_header(fdt_addr)) |
---|
569 | 485 | return -EBADF; |
---|
570 | 486 | |
---|
571 | | - *hash = file->hash; |
---|
572 | | - *hash_size = file->hash_size; |
---|
573 | | - printf("DTB: %s\n", file->name); |
---|
| 487 | + *hash = f->hash; |
---|
| 488 | + *hash_size = f->hash_size; |
---|
574 | 489 | |
---|
575 | | - return 0; |
---|
576 | | -} |
---|
577 | | - |
---|
578 | | -int resource_traverse_init_list(void) |
---|
579 | | -{ |
---|
580 | | - if (!resource_is_empty()) |
---|
581 | | - return 0; |
---|
582 | | - |
---|
583 | | -#ifdef CONFIG_ROCKCHIP_FIT_IMAGE |
---|
584 | | - if (!fit_image_init_resource()) |
---|
585 | | - return 0; |
---|
586 | | -#endif |
---|
587 | | -#ifdef CONFIG_ROCKCHIP_UIMAGE |
---|
588 | | - if (!uimage_init_resource()) |
---|
589 | | - return 0; |
---|
590 | | -#endif |
---|
591 | | - /* Android image is default supported within resource core */ |
---|
| 490 | + printf("DTB: %s\n", f->name); |
---|
592 | 491 | |
---|
593 | 492 | return 0; |
---|
594 | 493 | } |
---|
.. | .. |
---|
596 | 495 | static int do_dump_resource(cmd_tbl_t *cmdtp, int flag, |
---|
597 | 496 | int argc, char *const argv[]) |
---|
598 | 497 | { |
---|
599 | | - struct resource_file *file; |
---|
| 498 | + struct resource_file *f; |
---|
600 | 499 | struct list_head *node; |
---|
601 | 500 | |
---|
602 | | - printf("Resources:\n"); |
---|
603 | | - list_for_each(node, &entrys_head) { |
---|
604 | | - file = list_entry(node, struct resource_file, link); |
---|
605 | | - printf(" %s: 0x%08x(sector), 0x%08x(bytes)\n", |
---|
606 | | - file->name, file->rsce_base + file->f_offset, file->f_size); |
---|
| 501 | + list_for_each(node, &entry_head) { |
---|
| 502 | + f = list_entry(node, struct resource_file, link); |
---|
| 503 | + resource_dump(f); |
---|
607 | 504 | } |
---|
608 | 505 | |
---|
609 | | -#ifdef CONFIG_ROCKCHIP_HWID_DTB |
---|
610 | | - printf("DTBs:\n"); |
---|
611 | | - list_for_each(node, &entrys_dtbs_head) { |
---|
612 | | - file = list_entry(node, struct resource_file, dtbs); |
---|
613 | | - printf(" %s: 0x%08x(sector),0x%08x(bytes)\n", |
---|
614 | | - file->name, file->rsce_base + file->f_offset, file->f_size); |
---|
615 | | - } |
---|
616 | | -#endif |
---|
617 | 506 | return 0; |
---|
618 | 507 | } |
---|
619 | 508 | |
---|
620 | 509 | U_BOOT_CMD( |
---|
621 | 510 | dump_resource, 1, 1, do_dump_resource, |
---|
622 | | - "dump resource list", |
---|
| 511 | + "dump resource files", |
---|
623 | 512 | "" |
---|
624 | 513 | ); |
---|
625 | 514 | |
---|