hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * (C) Copyright 2017 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
#include <common.h>
#include <boot_rkimg.h>
#include <bmp_layout.h>
#include <malloc.h>
#include <asm/unaligned.h>
#include <linux/libfdt.h>
#include <linux/list.h>
#include <asm/arch/resource_img.h>
#include <asm/arch/uimage.h>
#include <asm/arch/fit.h>
 
DECLARE_GLOBAL_DATA_PTR;
 
#define PART_RESOURCE            "resource"
#define RESOURCE_MAGIC            "RSCE"
#define RESOURCE_MAGIC_SIZE        4
#define ENTRY_TAG            "ENTR"
#define ENTRY_TAG_SIZE            4
#define MAX_FILE_NAME_LEN        220
#define MAX_HASH_LEN            32
#define DEFAULT_DTB_FILE        "rk-kernel.dtb"
 
/*
 *         resource image structure
 * ----------------------------------------------
 * |                                            |
 * |    header  (1 block)                       |
 * |                                            |
 * ---------------------------------------------|
 * |                      |                     |
 * |    entry0  (1 block) |                     |
 * |                      |                     |
 * ------------------------                     |
 * |                      |                     |
 * |    entry1  (1 block) | contents (n blocks) |
 * |                      |                     |
 * ------------------------                     |
 * |    ......            |                     |
 * ------------------------                     |
 * |                      |                     |
 * |    entryn  (1 block) |                     |
 * |                      |                     |
 * ----------------------------------------------
 * |                                            |
 * |    file0  (x blocks)                       |
 * |                                            |
 * ----------------------------------------------
 * |                                            |
 * |    file1  (y blocks)                       |
 * |                                            |
 * ----------------------------------------------
 * |                   ......                   |
 * |---------------------------------------------
 * |                                            |
 * |    filen  (z blocks)                       |
 * |                                            |
 * ----------------------------------------------
 */
 
/**
 * struct resource_img_hdr
 *
 * @magic: should be "RSCE"
 * @version: resource image version, current is 0
 * @c_version: content version, current is 0
 * @blks: the size of the header ( 1 block = 512 bytes)
 * @c_offset: contents offset(by block) in the image
 * @e_blks: the size(by block) of the entry in the contents
 * @e_num: numbers of the entrys.
 */
struct resource_img_hdr {
   char        magic[4];
   uint16_t    version;
   uint16_t    c_version;
   uint8_t        blks;
   uint8_t        c_offset;
   uint8_t        e_blks;
   uint32_t    e_nums;
};
 
struct resource_entry {
   char        tag[4];
   char        name[MAX_FILE_NAME_LEN];
   char        hash[MAX_HASH_LEN];
   uint32_t    hash_size;
   uint32_t    blk_offset;
   uint32_t    size;        /* in byte */
};
 
LIST_HEAD(entry_head);
 
static int resource_check_header(struct resource_img_hdr *hdr)
{
   return memcmp(RESOURCE_MAGIC, hdr->magic, RESOURCE_MAGIC_SIZE);
}
 
static void resource_dump(struct resource_file *f)
{
   printf("%s\n", f->name);
   printf("  blk_start:  0x%08lx\n", (ulong)f->blk_start);
   printf("  blk_offset: 0x%08lx\n", (ulong)f->blk_offset);
   printf("  size:       0x%08x\n", f->size);
   printf("  in_ram:     %d\n", f->in_ram);
   printf("  hash_size:  %d\n\n", f->hash_size);
}
 
static int resource_add_file(const char *name, u32 size,
                u32 blk_start,  u32 blk_offset,
                char *hash, u32 hash_size,
                bool in_ram)
{
   struct resource_file *f;
   struct list_head *node;
   bool _new = true;
 
   /* old one ? */
   list_for_each(node, &entry_head) {
       f = list_entry(node, struct resource_file, link);
       if (!strcmp(f->name, name)) {
           _new = false;
           break;
       }
   }
 
   if (_new) {
       f = calloc(1, sizeof(*f));
       if (!f)
           return -ENOMEM;
 
       list_add_tail(&f->link, &entry_head);
   }
 
   strcpy(f->name, name);
   f->size       = size;
   f->in_ram     = in_ram;
   f->blk_start  = blk_start;
   f->blk_offset = blk_offset;
   f->hash_size  = hash_size;
   memcpy(f->hash, hash, hash_size);
#ifdef DEBUG
   resource_dump(f);
#endif
   return 0;
}
 
static int resource_setup_list(struct blk_desc *desc, ulong blk_start,
                  void *resc_hdr, bool in_ram)
{
   struct resource_img_hdr *hdr = resc_hdr;
   struct resource_entry *et;
   u32 i, stride;
   void *pos;
 
   pos = (void *)hdr + hdr->c_offset * desc->blksz;
   stride = hdr->e_blks * desc->blksz;
 
   for (i = 0; i < hdr->e_nums; i++) {
       et = pos + (i * stride);
       if (memcmp(et->tag, ENTRY_TAG, ENTRY_TAG_SIZE))
           continue;
 
       resource_add_file(et->name, et->size,
                 blk_start, et->blk_offset,
                 et->hash, et->hash_size, in_ram);
   }
 
   return 0;
}
 
int resource_setup_ram_list(struct blk_desc *desc, void *hdr)
{
   if (!desc)
       return -ENODEV;
 
   if (resource_check_header(hdr)) {
       printf("RESC: invalid\n");
       return -EINVAL;
   }
 
   /* @blk_start: set as 'hdr' point addr, to be used in byte */
   return resource_setup_list(desc, (ulong)hdr, hdr, true);
}
 
#ifdef CONFIG_ANDROID_BOOT_IMAGE
/*
 * Add logo.bmp and logo_kernel.bmp from "logo" parititon
 *
 * Provide a "logo" partition for user to store logo.bmp and
 * logo_kernel.bmp, so that the user can update them from
 * kernel or user-space dynamically.
 *
 * "logo" partition layout, do not change order:
 *
 *   |----------------------| 0x00
 *   | raw logo.bmp        |
 *   |----------------------| -> 512-byte aligned
 *   | raw logo_kernel.bmp  |
 *   |----------------------|
 *
 * N: the sector count of logo.bmp
 *
 * How to generate:
 *     cat logo.bmp > logo.img && truncate -s %512 logo.img && cat logo_kernel.bmp >> logo.img
 */
static int resource_setup_logo_bmp(struct blk_desc *desc)
{
   struct bmp_header *header;
   const char *name[] = { "logo.bmp", "logo_kernel.bmp" };
   disk_partition_t part;
   u32 blk_offset = 0;
   u32 filesz;
   int ret, i;
 
   if (part_get_info_by_name(desc, PART_LOGO, &part) < 0)
       return 0;
 
   header = memalign(ARCH_DMA_MINALIGN, desc->blksz);
   if (!header)
       return -ENOMEM;
 
   for (i = 0; i < ARRAY_SIZE(name); i++) {
       if (blk_dread(desc, part.start + blk_offset, 1, header) != 1) {
           ret = -EIO;
           break;
       }
 
       if (header->signature[0] != 'B' || header->signature[1] != 'M') {
           ret = -EINVAL;
           break;
       }
 
       filesz = get_unaligned_le32(&header->file_size);
       ret = resource_add_file(name[i], filesz, part.start, blk_offset,
                   NULL, 0, false);
       if (ret)
           break;
 
       /* move to next file */
       blk_offset += DIV_ROUND_UP(filesz, desc->blksz);
 
       printf("LOGO: %s\n", name[i]);
 
   }
 
   free(header);
 
   return ret;
}
 
static int resource_setup_blk_list(struct blk_desc *desc, ulong blk_start)
{
   struct resource_img_hdr *hdr;
   int blk_cnt;
   int ret = 0;
   void *buf;
 
   hdr = memalign(ARCH_DMA_MINALIGN, desc->blksz);
   if (!hdr)
       return -ENOMEM;
 
   if (blk_dread(desc, blk_start, 1, hdr) != 1) {
       ret = -EIO;
       goto out;
   }
 
   if (resource_check_header(hdr)) {
       printf("RESC: invalid\n");
       if (fdt_check_header(hdr)) {
           ret = -EINVAL;
           goto out;
       } else {
           /* this is a dtb file */
           printf("RESC: this is dtb\n");
           ret = resource_add_file(DEFAULT_DTB_FILE,
                       fdt_totalsize(hdr),
                       blk_start, 0, NULL, 0, false);
           goto out;
       }
   }
 
   blk_cnt = hdr->e_blks * hdr->e_nums;
   hdr = realloc(hdr, (1 + blk_cnt) * desc->blksz);
   if (!hdr) {
       ret = -ENOMEM;
       goto out;
   }
 
   buf = (void *)hdr + desc->blksz;
   if (blk_dread(desc, blk_start + hdr->c_offset, blk_cnt, buf) != blk_cnt) {
       ret = -EIO;
       goto out;
   }
 
   resource_setup_list(desc, blk_start, hdr, false);
   resource_setup_logo_bmp(desc);
out:
   free(hdr);
 
   return ret;
}
 
static int resource_init(struct blk_desc *desc,
            disk_partition_t *part,
            ulong blk_offset)
{
   printf("RESC: '%s', blk@0x%08lx\n", part->name, part->start + blk_offset);
 
#ifdef CONFIG_ANDROID_AVB
   char hdr[512];
   ulong resc_buf = 0;
   int ret;
 
   if (blk_dread(desc, part->start, 1, hdr) != 1)
       return -EIO;
 
   /* only handle android boot/recovery.img and resource.img, ignore fit */
   if (!android_image_check_header((void *)hdr) ||
       !resource_check_header((void *)hdr)) {
       ret = android_image_verify_resource((const char *)part->name, &resc_buf);
       if (ret) {
           printf("RESC: '%s', avb verify fail: %d\n", part->name, ret);
           return ret;
       }
 
       /*
        * unlock=0: resc_buf is valid and file was already full load in ram.
        * unlock=1: resc_buf is 0.
        */
       if (resc_buf && !resource_check_header((void *)resc_buf))
           return resource_setup_ram_list(desc, (void *)resc_buf);
   }
#endif
 
   return resource_setup_blk_list(desc, part->start + blk_offset);
}
 
static int resource_default(struct blk_desc *desc,
               disk_partition_t *out_part,
               ulong *out_blk_offset)
{
   disk_partition_t part;
 
   if (part_get_info_by_name(desc, PART_RESOURCE, &part) < 0)
       return -ENODEV;
 
   *out_part = part;
   *out_blk_offset = 0;
 
   return 0;
}
#endif
 
static int resource_scan(void)
{
   struct blk_desc *desc = rockchip_get_bootdev();
   __maybe_unused int ret;
 
   if (!desc) {
       printf("RESC: No bootdev\n");
       return -ENODEV;
   }
 
   if (!list_empty(&entry_head))
       return 0;
 
#ifdef CONFIG_ROCKCHIP_FIT_IMAGE
   ret = fit_image_init_resource(desc);
   if (!ret || ret != -EAGAIN)
       return ret;
#endif
#ifdef CONFIG_ROCKCHIP_UIMAGE
   ret = uimage_init_resource(desc);
   if (!ret || ret != -EAGAIN)
       return ret;
#endif
#ifdef CONFIG_ANDROID_BOOT_IMAGE
   disk_partition_t part;
   ulong blk_offset;
   char hdr[512];
   char name[32];
 
   /* partition priority: boot/recovery > resource */
   if (!android_image_init_resource(desc, &part, &blk_offset)) {
       if (blk_dread(desc, part.start + blk_offset, 1, hdr) != 1)
           return -EIO;
 
       if (resource_check_header((void *)hdr)) {
           strcpy(name, (char *)part.name);
           if (resource_default(desc, &part, &blk_offset))
               return -ENOENT;
 
           printf("RESC: '%s' -> '%s'\n", name, part.name);
       }
   } else {
       if (resource_default(desc, &part, &blk_offset))
           return -ENOENT;
   }
 
   /* now, 'part' can be boot/recovery/resource */
   return resource_init(desc, &part, blk_offset);
#endif
   return -ENOENT;
}
 
static struct resource_file *resource_get_file(const char *name)
{
   struct resource_file *f;
   struct list_head *node;
 
   if (resource_scan())
       return NULL;
 
   list_for_each(node, &entry_head) {
       f = list_entry(node, struct resource_file, link);
       if (!strcmp(f->name, name))
           return f;
   }
 
   return NULL;
}
 
int rockchip_read_resource_file(void *buf, const char *name, int blk_offset, int len)
{
   struct blk_desc *desc = rockchip_get_bootdev();
   struct resource_file *f;
   int blk_cnt;
   ulong pos;
 
   if (!desc)
       return -ENODEV;
 
   f = resource_get_file(name);
   if (!f) {
       printf("No resource file: %s\n", name);
       return -ENOENT;
   }
 
   if (len <= 0 || len > f->size)
       len = f->size;
 
   if (f->in_ram) {
       pos = f->blk_start + (f->blk_offset + blk_offset) * desc->blksz;
       memcpy(buf, (char *)pos, len);
   } else {
       blk_cnt = DIV_ROUND_UP(len, desc->blksz);
       if (blk_dread(desc,
                 f->blk_start + f->blk_offset + blk_offset,
                 blk_cnt, buf) != blk_cnt)
           len = -EIO;
   }
 
   return len;
}
 
extern struct resource_file *resource_read_hwid_dtb(void);
 
int rockchip_read_resource_dtb(void *fdt_addr, char **hash, int *hash_size)
{
   struct resource_file *f = NULL;
   int ret;
 
#ifdef CONFIG_ROCKCHIP_HWID_DTB
   if (resource_scan())
       return -ENOENT;
 
   f = resource_read_hwid_dtb();
#endif
   /* If no dtb match hardware id(GPIO/ADC), use the default */
   if (!f)
       f = resource_get_file(DEFAULT_DTB_FILE);
 
   if (!f)
       return -ENODEV;
 
   ret = rockchip_read_resource_file(fdt_addr, f->name, 0, 0);
   if (ret < 0)
       return ret;
 
   if (fdt_check_header(fdt_addr))
       return -EBADF;
 
   *hash = f->hash;
   *hash_size = f->hash_size;
 
   printf("DTB: %s\n", f->name);
 
   return 0;
}
 
static int do_dump_resource(cmd_tbl_t *cmdtp, int flag,
               int argc, char *const argv[])
{
   struct resource_file *f;
   struct list_head *node;
 
   list_for_each(node, &entry_head) {
       f = list_entry(node, struct resource_file, link);
       resource_dump(f);
   }
 
   return 0;
}
 
U_BOOT_CMD(
   dump_resource, 1, 1, do_dump_resource,
   "dump resource files",
   ""
);