forked from thesofproject/rimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadsp_config.c
More file actions
2671 lines (2209 loc) · 74.4 KB
/
Copy pathadsp_config.c
File metadata and controls
2671 lines (2209 loc) · 74.4 KB
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2020 Intel Corporation. All rights reserved.
*
* Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
*/
#include "rimage/sof/user/manifest.h"
#include "rimage/sof/user/manifest.h"
#include "rimage/adsp_config.h"
#include "rimage/ext_manifest_gen.h"
#include "rimage/plat_auth.h"
#include "rimage/manifest.h"
#include "rimage/rimage.h"
#include "rimage/cse.h"
#include "rimage/css.h"
#include "toml.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
/* macros used to dump values after parsing */
#define DUMP_KEY_FMT " %20s: "
#define DUMP(fmt, ...) fprintf(stdout, fmt "\n", ##__VA_ARGS__)
#define DUMP_KEY(key, fmt, ...) DUMP(DUMP_KEY_FMT fmt, key, ##__VA_ARGS__)
static void print_bytes(FILE *out, const uint8_t *arr, size_t len)
{
for (const uint8_t *pos = arr; pos < arr + len; pos++) {
char c = *pos;
if (isprint(c))
fputc(c, out);
else
fprintf(out, "\\x%.2x", c);
}
}
#define DUMP_PRINTABLE_BYTES(name, var) _dump_printable_bytes(name, var, sizeof(var))
static void _dump_printable_bytes(const char *name, const uint8_t *arr, size_t len)
{
printf(DUMP_KEY_FMT, name);
print_bytes(stdout, arr, len);
printf("\n");
}
/** parser counter, used to assert nothing left unparsed in toml data */
struct parse_ctx {
int key_cnt; /**< number of parsed key */
int table_cnt; /**< number of parsed tables */
int array_cnt; /**< number of parsed arrays */
};
/** private parser error trace function */
static void vlog_err(const char *msg, va_list vl)
{
vfprintf(stderr, msg, vl);
}
/** parser error trace function, error code is returned to shorten client code */
static int log_err(int err_code, const char *msg, ...)
{
va_list vl;
va_start(vl, msg);
vlog_err(msg, vl);
va_end(vl);
return err_code;
}
/** log malloc error message for given key */
static int err_malloc(const char *key)
{
return log_err(-ENOMEM, "error: malloc failed during parsing key '%s'\n", key);
}
/** log key not found error */
static int err_key_not_found(const char *key)
{
return log_err(-EINVAL, "error: '%s' not found\n", key);
}
/** error during parsing key value, possible detailed message */
static int err_key_parse(const char *key, const char *extra_msg, ...)
{
int ret = -EINVAL;
va_list vl;
if (extra_msg) {
log_err(ret, "error: key '%s' parsing error, ", key);
va_start(vl, extra_msg);
vlog_err(extra_msg, vl);
va_end(vl);
return log_err(ret, "\n");
} else {
return log_err(ret, "error: key '%s' parsing error\n", key);
}
}
/** initialize parser context before parsing */
static void parse_ctx_init(struct parse_ctx *ctx)
{
memset(ctx, 0, sizeof(*ctx));
}
/** check nothing left unparsed in given parsing context */
static int assert_everything_parsed(const toml_table_t *table, struct parse_ctx *ctx)
{
const char *key = toml_table_key(table);
int ret = 0;
/* toml_table_key returns NULL for global context */
if (!key)
key = "toml";
/* from number of parsed fields subtract fields count in given table */
ctx->key_cnt = toml_table_nkval(table) - ctx->key_cnt;
ctx->array_cnt = toml_table_narr(table) - ctx->array_cnt;
ctx->table_cnt = toml_table_ntab(table) - ctx->table_cnt;
/* when eny field left unparsed, then raise error */
if (ctx->key_cnt != 0)
ret = log_err(-EINVAL, "error: %d unparsed keys left in '%s'\n", ctx->key_cnt, key);
if (ctx->array_cnt != 0)
ret = log_err(-EINVAL, "error: %d unparsed arrays left in '%s'\n", ctx->array_cnt,
key);
if (ctx->table_cnt != 0)
ret = log_err(-EINVAL, "error: %d unparsed tables left in '%s'\n", ctx->table_cnt,
key);
return ret;
}
/**
* Parse hex value from key in given toml table
* @param table toml table where key is specified
* @param ctx parsing context, key counter will be incremented after successful key parse
* @param key field name
* @param def is default value or -1 when value don't have default value
* @param error code, 0 when success
* @return default, parsed, or UINT32_MAX value for error cases
*/
static uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
const char *key, int64_t def, int *error)
{
toml_raw_t raw;
char *temp_s;
uint32_t val;
int ret;
/* look for key in given table, assign def value when key not found */
raw = toml_raw_in(table, key);
if (!raw) {
if (def < 0 || def > UINT32_MAX) {
*error = err_key_not_found(key);
return UINT32_MAX;
} else {
*error = 0;
return (uint32_t)def;
}
}
/* there is not build-in support for hex numbers in toml, so read then as string */
ret = toml_rtos(raw, &temp_s);
if (ret < 0) {
*error = err_key_parse(key, NULL);
return UINT32_MAX;
}
val = strtoul(temp_s, 0, 16);
free(temp_s);
/* assert parsing success and value is within uint32_t range */
if (errno < 0) {
*error = err_key_parse(key, "can't convert hex value");
return UINT32_MAX;
}
/* set success error code and increment parsed key counter */
*error = 0;
++ctx->key_cnt;
return (uint32_t)val;
}
/**
* Parse integer value from key in given toml table
* @param table toml table where key is specified
* @param ctx parsing context, key counter will be incremented after successful key parse
* @param key field name
* @param def is default value or -1 when value don't have default value
* @param error code, 0 when success
* @return default, parsed, or UINT32_MAX value for error cases
*/
static uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
int64_t def, int *error)
{
toml_raw_t raw;
int64_t val;
int ret;
/* look for key in given table, assign def value when key not found */
raw = toml_raw_in(table, key);
if (!raw) {
if (def < 0 || def > UINT32_MAX) {
*error = err_key_not_found(key);
return UINT32_MAX;
} else {
*error = 0;
return (uint32_t)def;
}
}
/* there is build-in support for integer numbers in toml, so use lib function */
ret = toml_rtoi(raw, &val);
if (ret < 0) {
*error = err_key_parse(key, "can't convert to integer value");
return UINT32_MAX;
}
/* assert value is within uint32_t range */
if (val < 0 || val > UINT32_MAX) {
*error = log_err(-ERANGE, "key %s out of uint32_t range", key);
return UINT32_MAX;
}
/* set success error code and increment parsed key counter */
*error = 0;
++ctx->key_cnt;
return (uint32_t)val;
}
/**
* Parse string value from key in given toml table to uint8_t array. The
* destination is NOT a string because it is padded with zeros if and
* only if there is some capacity left. For string destinations use
* parse_str_key().
*
* @param table toml table where key is specified
* @param ctx parsing context, key counter will be incremented after successful key parse
* @param key field name
* @param dst uint8_t[] destination
* @param capacity dst array size
* @param error code, 0 when success
*/
static void parse_printable_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
uint8_t *dst, int capacity, int *error)
{
toml_raw_t raw;
char *temp_s;
int len;
int ret;
/* look for key in given table */
raw = toml_raw_in(table, key);
if (!raw) {
*error = err_key_not_found(key);
return;
}
/* read string from toml, theres malloc inside toml_rtos() */
ret = toml_rtos(raw, &temp_s);
if (ret < 0) {
*error = err_key_parse(key, NULL);
return;
}
len = strlen(temp_s);
if (len > capacity) {
if (len > 20) {
static const char ellipsis[] = "...";
const size_t el_len = sizeof(ellipsis);
strncpy(temp_s + 20 - el_len, ellipsis, el_len);
}
*error = log_err(-EINVAL, "Too long input '%s' for key '%s' (%d > %d) characters\n",
temp_s, key, len, capacity);
free(temp_s);
return;
}
/* copy string to dst, pad with zeros the space left if any */
strncpy((char *)dst, temp_s, capacity);
free(temp_s);
/* update parsing context */
++ctx->key_cnt;
*error = 0;
}
/**
* Parse string value from key in given toml table to given
* char[]. Destination is padded with zeros. As the only difference with
* parse_printable_key(), dst is guaranteed to be null-terminated when
* there is no error because the last destination byte is reserved for
* that.
*
* @param table toml table where key is specified
* @param ctx parsing context, key counter will be incremented after successful key parse
* @param key field name
* @param dst char[] destination
* @param capacity dst array size including null termination.
* @param error code, 0 when success
*/
static void parse_str_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
char *dst, int capacity, int *error)
{
parse_printable_key(table, ctx, key, (uint8_t *)dst, capacity - 1, error);
if (*error) /* return immediately to help forensics */
return;
dst[capacity - 1] = 0;
}
/* map memory zone string name to enum value */
static enum snd_sof_fw_blk_type zone_name_to_idx(const char *name)
{
static const struct {
const char name[32];
enum snd_sof_fw_blk_type type;
} mem_zone_name_dict[] = {
{"START", SOF_FW_BLK_TYPE_START},
{"IRAM", SOF_FW_BLK_TYPE_IRAM},
{"DRAM", SOF_FW_BLK_TYPE_DRAM},
{"SRAM", SOF_FW_BLK_TYPE_SRAM},
{"ROM", SOF_FW_BLK_TYPE_ROM},
{"IMR", SOF_FW_BLK_TYPE_IMR},
{"RSRVD0", SOF_FW_BLK_TYPE_RSRVD0},
{"HP-SRAM", SOF_FW_BLK_TYPE_HPSRAM},
{"LP-SRAM", SOF_FW_BLK_TYPE_LPSRAM},
{"RSRVD8", SOF_FW_BLK_TYPE_RSRVD8},
{"RSRVD9", SOF_FW_BLK_TYPE_RSRVD9},
{"RSRVD10", SOF_FW_BLK_TYPE_RSRVD10},
{"RSRVD11", SOF_FW_BLK_TYPE_RSRVD11},
{"RSRVD12", SOF_FW_BLK_TYPE_RSRVD12},
{"RSRVD13", SOF_FW_BLK_TYPE_RSRVD13},
{"RSRVD14", SOF_FW_BLK_TYPE_RSRVD14},
};
int i;
for (i = 0; i < ARRAY_SIZE(mem_zone_name_dict); ++i) {
if (!strcmp(name, mem_zone_name_dict[i].name))
return mem_zone_name_dict[i].type;
}
return SOF_FW_BLK_TYPE_INVALID;
}
static void dump_adsp(const struct adsp *adsp)
{
int i;
DUMP("\nadsp");
DUMP_KEY("name", "'%s'", adsp->name);
DUMP_KEY("image_size", "0x%x", adsp->image_size);
DUMP_KEY("dram_offset", "0x%x", adsp->dram_offset);
DUMP_KEY("exec_boot_ldr", "%d", adsp->exec_boot_ldr);
for (i = 0; i < ARRAY_SIZE(adsp->mem_zones); ++i) {
DUMP_KEY("mem_zone.idx", "%d", i);
DUMP_KEY("mem_zone.size", "0x%x", adsp->mem_zones[i].size);
DUMP_KEY("mem_zone.base", "0x%x", adsp->mem_zones[i].base);
DUMP_KEY("mem_zone.host_offset", "0x%x", adsp->mem_zones[i].host_offset);
}
}
static int parse_adsp(const toml_table_t *toml, struct parse_ctx *pctx, struct adsp *out,
bool verbose)
{
toml_array_t *mem_zone_array;
toml_table_t *mem_zone;
struct mem_zone *zone;
struct parse_ctx ctx;
char zone_name[32];
toml_table_t *adsp;
toml_raw_t raw;
int zone_idx;
int ret;
int i;
/* look for subtable in toml, increment pctx parsed table cnt and initialize local ctx */
adsp = toml_table_in(toml, "adsp");
if (!adsp)
return err_key_not_found("adsp");
++pctx->table_cnt;
parse_ctx_init(&ctx);
/* non-configurable fields */
/* configurable fields */
raw = toml_raw_in(adsp, "name");
if (!raw)
return err_key_not_found("name");
++ctx.key_cnt;
/* free(out->name) is called in adsp_free() */
ret = toml_rtos(raw, (char **)&out->name);
if (ret < 0)
return err_key_parse("name", NULL);
out->image_size = parse_uint32_hex_key(adsp, &ctx, "image_size", 0, &ret);
if (ret < 0)
return ret;
out->dram_offset = parse_uint32_hex_key(adsp, &ctx, "dram_offset", 0, &ret);
if (ret < 0)
return ret;
out->exec_boot_ldr = parse_uint32_key(adsp, &ctx, "exec_boot_ldr", 0, &ret);
if (ret < 0)
return ret;
/* check everything parsed, 1 table should be present */
ctx.array_cnt += 1;
ret = assert_everything_parsed(adsp, &ctx);
if (ret < 0)
return ret;
/* look for entry array */
memset(out->mem_zones, 0, sizeof(out->mem_zones));
mem_zone_array = toml_array_in(adsp, "mem_zone");
if (!mem_zone_array)
return err_key_not_found("mem_zone");
if (toml_array_kind(mem_zone_array) != 't' ||
toml_array_nelem(mem_zone_array) > SOF_FW_BLK_TYPE_NUM)
return err_key_parse("mem_zone", "wrong array type or length > %d",
SOF_FW_BLK_TYPE_NUM);
/* parse entry array elements */
for (i = 0; i < toml_array_nelem(mem_zone_array); ++i) {
mem_zone = toml_table_at(mem_zone_array, i);
if (!mem_zone)
return err_key_parse("mem_zone", NULL);
/* initialize parse context for each array element */
parse_ctx_init(&ctx);
/* non-configurable fields */
/* configurable fields */
parse_str_key(mem_zone, &ctx, "type", zone_name, sizeof(zone_name), &ret);
if (ret < 0)
return err_key_parse("mem_zone", NULL);
zone_idx = zone_name_to_idx(zone_name);
if (zone_idx < 0)
return err_key_parse("mem_zone.name", "unknown zone '%s'", zone_name);
zone = &out->mem_zones[zone_idx];
zone->base = parse_uint32_hex_key(mem_zone, &ctx, "base", -1, &ret);
if (ret < 0)
return err_key_parse("mem_zone", NULL);
zone->host_offset = parse_uint32_hex_key(mem_zone, &ctx, "host_offset", 0,
&ret);
if (ret < 0)
return err_key_parse("mem_zone", NULL);
zone->size = parse_uint32_hex_key(mem_zone, &ctx, "size", -1, &ret);
if (ret < 0)
return err_key_parse("mem_zone", NULL);
/* check everything parsed */
ret = assert_everything_parsed(mem_zone, &ctx);
if (ret < 0)
return ret;
}
if (verbose)
dump_adsp(out);
/*
* values set in other places in code:
* - write_firmware
* - write_firmware_meu
* - man_vX_Y
*/
return 0;
}
static void dump_cse(const struct CsePartitionDirHeader *cse_header,
const struct CsePartitionDirEntry *cse_entry)
{
int i;
DUMP("\ncse");
DUMP_PRINTABLE_BYTES("partition_name", cse_header->partition_name);
DUMP_KEY("header_version", "%d", cse_header->header_version);
DUMP_KEY("entry_version", "%d", cse_header->entry_version);
DUMP_KEY("nb_entries", "%d", cse_header->nb_entries);
for (i = 0; i < cse_header->nb_entries; ++i) {
DUMP_PRINTABLE_BYTES("entry.name", cse_entry[i].entry_name);
DUMP_KEY("entry.offset", "0x%x", cse_entry[i].offset);
DUMP_KEY("entry.length", "0x%x", cse_entry[i].length);
}
}
static int parse_cse(const toml_table_t *toml, struct parse_ctx *pctx,
struct CsePartitionDirHeader *hdr, struct CsePartitionDirEntry *out,
int entry_capacity, bool verbose)
{
toml_array_t *cse_entry_array;
toml_table_t *cse_entry;
struct parse_ctx ctx;
toml_table_t *cse;
int ret;
int i;
/* look for subtable in toml, increment pctx parsed table cnt and initialize local ctx */
cse = toml_table_in(toml, "cse");
if (!cse)
return err_key_not_found("cse");
++pctx->table_cnt;
parse_ctx_init(&ctx);
/* non-configurable fields */
hdr->header_marker = CSE_HEADER_MAKER;
hdr->header_length = sizeof(struct CsePartitionDirHeader);
/* configurable fields */
hdr->header_version = parse_uint32_key(cse, &ctx, "header_version", 1, &ret);
if (ret < 0)
return ret;
hdr->entry_version = parse_uint32_key(cse, &ctx, "entry_version", 1, &ret);
if (ret < 0)
return ret;
parse_printable_key(cse, &ctx, "partition_name", hdr->partition_name,
sizeof(hdr->partition_name), &ret);
if (ret < 0)
return ret;
/* check everything parsed, expect 1 table */
ctx.array_cnt += 1;
ret = assert_everything_parsed(cse, &ctx);
if (ret < 0)
return ret;
/* entry array */
cse_entry_array = toml_array_in(cse, "entry");
if (!cse_entry_array)
return err_key_not_found("entry");
if (toml_array_kind(cse_entry_array) != 't' ||
toml_array_nelem(cse_entry_array) != entry_capacity)
return err_key_parse("entry", "wrong array type or length != %d", entry_capacity);
/* parse entry array elements */
for (i = 0; i < toml_array_nelem(cse_entry_array); ++i) {
cse_entry = toml_table_at(cse_entry_array, i);
if (!cse_entry)
return err_key_parse("entry", NULL);
/* initialize parse context for each array element */
parse_ctx_init(&ctx);
/* non-configurable fields */
/* configurable fields */
parse_printable_key(cse_entry, &ctx, "name", out[i].entry_name,
sizeof(out[i].entry_name), &ret);
if (ret < 0)
return err_key_parse("entry", NULL);
out[i].offset = parse_uint32_hex_key(cse_entry, &ctx, "offset", -1, &ret);
if (ret < 0)
return err_key_parse("entry", NULL);
out[i].length = parse_uint32_hex_key(cse_entry, &ctx, "length", -1, &ret);
if (ret < 0)
return err_key_parse("entry", NULL);
/* check everything parsed */
ret = assert_everything_parsed(cse_entry, &ctx);
if (ret < 0)
return ret;
}
hdr->nb_entries = toml_array_nelem(cse_entry_array);
if (verbose)
dump_cse(hdr, out);
/*
* values set in other places in code:
* - checksum
*/
return 0;
}
static void dump_cse_v2_5(const struct CsePartitionDirHeader_v2_5 *cse_header,
const struct CsePartitionDirEntry *cse_entry)
{
int i;
DUMP("\ncse");
DUMP_PRINTABLE_BYTES("partition_name", cse_header->partition_name);
DUMP_KEY("header_version", "%d", cse_header->header_version);
DUMP_KEY("entry_version", "%d", cse_header->entry_version);
DUMP_KEY("nb_entries", "%d", cse_header->nb_entries);
for (i = 0; i < cse_header->nb_entries; ++i) {
DUMP_PRINTABLE_BYTES("entry.name", cse_entry[i].entry_name);
DUMP_KEY("entry.offset", "0x%x", cse_entry[i].offset);
DUMP_KEY("entry.length", "0x%x", cse_entry[i].length);
}
}
/* TODO: fix up constants in headers for v2.5 */
static int parse_cse_v2_5(const toml_table_t *toml, struct parse_ctx *pctx,
struct CsePartitionDirHeader_v2_5 *hdr, struct CsePartitionDirEntry *out,
int entry_capacity, bool verbose)
{
toml_array_t *cse_entry_array;
toml_table_t *cse_entry;
struct parse_ctx ctx;
toml_table_t *cse;
int ret;
int i;
/* look for subtable in toml, increment pctx parsed table cnt and initialize local ctx */
cse = toml_table_in(toml, "cse");
if (!cse)
return err_key_not_found("cse");
++pctx->table_cnt;
parse_ctx_init(&ctx);
/* non-configurable fields */
hdr->header_marker = CSE_HEADER_MAKER;
hdr->header_length = sizeof(struct CsePartitionDirHeader_v2_5);
/* configurable fields */
hdr->header_version = parse_uint32_key(cse, &ctx, "header_version", 2, &ret);
if (ret < 0)
return ret;
hdr->entry_version = parse_uint32_key(cse, &ctx, "entry_version", 1, &ret);
if (ret < 0)
return ret;
parse_printable_key(cse, &ctx, "partition_name", hdr->partition_name,
sizeof(hdr->partition_name), &ret);
if (ret < 0)
return ret;
/* check everything parsed, expect 1 table */
ctx.array_cnt += 1;
ret = assert_everything_parsed(cse, &ctx);
if (ret < 0)
return ret;
/* entry array */
cse_entry_array = toml_array_in(cse, "entry");
if (!cse_entry_array)
return err_key_not_found("entry");
if (toml_array_kind(cse_entry_array) != 't' ||
toml_array_nelem(cse_entry_array) != entry_capacity)
return err_key_parse("entry", "wrong array type or length != %d", entry_capacity);
/* parse entry array elements */
for (i = 0; i < toml_array_nelem(cse_entry_array); ++i) {
cse_entry = toml_table_at(cse_entry_array, i);
if (!cse_entry)
return err_key_parse("entry", NULL);
/* initialize parse context for each array element */
parse_ctx_init(&ctx);
/* non-configurable fields */
/* configurable fields */
parse_printable_key(cse_entry, &ctx, "name", out[i].entry_name,
sizeof(out[i].entry_name), &ret);
if (ret < 0)
return err_key_parse("entry", NULL);
out[i].offset = parse_uint32_hex_key(cse_entry, &ctx, "offset", -1, &ret);
if (ret < 0)
return err_key_parse("offset", NULL);
out[i].length = parse_uint32_hex_key(cse_entry, &ctx, "length", -1, &ret);
if (ret < 0)
return err_key_parse("length", NULL);
/* check everything parsed */
ret = assert_everything_parsed(cse_entry, &ctx);
if (ret < 0)
return ret;
}
hdr->nb_entries = toml_array_nelem(cse_entry_array);
if (verbose)
dump_cse_v2_5(hdr, out);
/*
* values set in other places in code:
* - checksum
*/
return 0;
}
static void dump_css_v1_5(const struct css_header_v1_5 *css)
{
DUMP("\ncss 1.5");
DUMP_KEY("module_type", "%d", css->module_type);
DUMP_KEY("header_len", "%d", css->header_len);
DUMP_KEY("header_version", "0x%x", css->header_version);
DUMP_KEY("module_vendor", "0x%x", css->module_vendor);
DUMP_KEY("size", "%d", css->size);
DUMP_KEY("key_size", "%d", css->key_size);
DUMP_KEY("modulus_size", "%d", css->modulus_size);
DUMP_KEY("exponent_size", "%d", css->exponent_size);
}
static int parse_css_v1_5(const toml_table_t *toml, struct parse_ctx *pctx,
struct css_header_v1_5 *out, bool verbose)
{
struct parse_ctx ctx;
toml_table_t *css;
int ret;
/* look for subtable in toml, increment pctx parsed table cnt and initialize local ctx */
css = toml_table_in(toml, "css");
if (!css)
return err_key_not_found("css");
++pctx->table_cnt;
parse_ctx_init(&ctx);
/* non-configurable fields */
/* configurable fields */
out->module_type = parse_uint32_key(css, &ctx, "module_type", MAN_CSS_LT_MODULE_TYPE, &ret);
if (ret < 0)
return ret;
out->header_len = parse_uint32_key(css, &ctx, "header_len", MAN_CSS_HDR_SIZE, &ret);
if (ret < 0)
return ret;
out->header_version = parse_uint32_hex_key(css, &ctx, "header_version", MAN_CSS_HDR_VERSION,
&ret);
if (ret < 0)
return ret;
out->module_vendor = parse_uint32_hex_key(css, &ctx, "module_vendor", MAN_CSS_MOD_VENDOR,
&ret);
if (ret < 0)
return ret;
out->size = parse_uint32_key(css, &ctx, "size", 0x800, &ret);
if (ret < 0)
return ret;
out->key_size = parse_uint32_key(css, &ctx, "key_size", MAN_CSS_KEY_SIZE, &ret);
if (ret < 0)
return ret;
out->modulus_size = parse_uint32_key(css, &ctx, "modulus_size", MAN_CSS_MOD_SIZE, &ret);
if (ret < 0)
return ret;
out->exponent_size = parse_uint32_key(css, &ctx, "exponent_size", MAN_CSS_EXP_SIZE, &ret);
if (ret < 0)
return ret;
/* check everything parsed */
ret = assert_everything_parsed(css, &ctx);
if (ret < 0)
return ret;
if (verbose)
dump_css_v1_5(out);
/*
* values set in other places in code:
* - date
* - version
* - modulus
* - exponent
* - signature
*/
return 0;
}
static void dump_css_v1_8(const struct css_header_v1_8 *css)
{
DUMP("\ncss 1.8");
DUMP_KEY("header_type", "%d", css->header_type);
DUMP_KEY("header_len", "%d", css->header_len);
DUMP_KEY("header_version", "0x%x", css->header_version);
DUMP_KEY("module_vendor", "0x%x", css->module_vendor);
DUMP_KEY("size", "%d", css->size);
DUMP_KEY("svn", "%d", css->svn);
DUMP_KEY("modulus_size", "%d", css->modulus_size);
DUMP_KEY("exponent_size", "%d", css->exponent_size);
}
static int parse_css_v1_8(const toml_table_t *toml, struct parse_ctx *pctx,
struct css_header_v1_8 *out, bool verbose)
{
static const uint8_t hdr_id[4] = MAN_CSS_HDR_ID;
struct parse_ctx ctx;
toml_table_t *css;
int ret;
/* look for subtable in toml, increment pctx parsed table cnt and initialize local ctx */
css = toml_table_in(toml, "css");
if (!css)
return err_key_not_found("css");
++pctx->table_cnt;
parse_ctx_init(&ctx);
/* non-configurable fields */
memcpy(out->header_id, hdr_id, sizeof(out->header_id));
/* configurable fields */
out->header_type = parse_uint32_key(css, &ctx, "header_type", MAN_CSS_MOD_TYPE, &ret);
if (ret < 0)
return ret;
out->header_len = parse_uint32_key(css, &ctx, "header_len", MAN_CSS_HDR_SIZE, &ret);
if (ret < 0)
return ret;
out->header_version = parse_uint32_hex_key(css, &ctx, "header_version", MAN_CSS_HDR_VERSION,
&ret);
if (ret < 0)
return ret;
out->module_vendor = parse_uint32_hex_key(css, &ctx, "module_vendor", MAN_CSS_MOD_VENDOR,
&ret);
if (ret < 0)
return ret;
out->size = parse_uint32_key(css, &ctx, "size", 222, &ret);
if (ret < 0)
return ret;
out->svn = parse_uint32_key(css, &ctx, "svn", 0, &ret);
if (ret < 0)
return ret;
out->modulus_size = parse_uint32_key(css, &ctx, "modulus_size", MAN_CSS_MOD_SIZE, &ret);
if (ret < 0)
return ret;
out->exponent_size = parse_uint32_key(css, &ctx, "exponent_size", MAN_CSS_EXP_SIZE, &ret);
if (ret < 0)
return ret;
/* check everything parsed */
ret = assert_everything_parsed(css, &ctx);
if (ret < 0)
return ret;
if (verbose)
dump_css_v1_8(out);
/*
* values set in other places in code:
* - date
* - version
* - modulus
* - exponent
* - signature
*/
return 0;
}
static void dump_css_v2_5(const struct css_header_v2_5 *css)
{
DUMP("\ncss 2.5");
DUMP_KEY("header_type", "%d", css->header_type);
DUMP_KEY("header_len", "%d", css->header_len);
DUMP_KEY("header_version", "0x%x", css->header_version);
DUMP_KEY("module_vendor", "0x%x", css->module_vendor);
DUMP_KEY("size", "%d", css->size);
DUMP_KEY("svn", "%d", css->svn);
DUMP_KEY("modulus_size", "%d", css->modulus_size);
DUMP_KEY("exponent_size", "%d", css->exponent_size);
}
static int parse_css_v2_5(const toml_table_t *toml, struct parse_ctx *pctx,
struct css_header_v2_5 *out, bool verbose)
{
static const uint8_t hdr_id[4] = MAN_CSS_HDR_ID;
struct parse_ctx ctx;
toml_table_t *css;
int ret;
/* look for subtable in toml, increment pctx parsed table cnt and initialize local ctx */
css = toml_table_in(toml, "css");
if (!css)
return err_key_not_found("css");
++pctx->table_cnt;
parse_ctx_init(&ctx);
/* non-configurable fields */
memcpy(out->header_id, hdr_id, sizeof(out->header_id));
/* configurable fields */
out->header_type = parse_uint32_key(css, &ctx, "header_type", MAN_CSS_MOD_TYPE, &ret);
if (ret < 0)
return ret;
out->header_len = parse_uint32_key(css, &ctx, "header_len", MAN_CSS_HDR_SIZE_2_5, &ret);
if (ret < 0)
return ret;
out->header_version = parse_uint32_hex_key(css, &ctx, "header_version", MAN_CSS_HDR_VERSION_2_5,
&ret);
if (ret < 0)
return ret;
out->module_vendor = parse_uint32_hex_key(css, &ctx, "module_vendor", MAN_CSS_MOD_VENDOR,
&ret);
if (ret < 0)
return ret;
out->size = parse_uint32_key(css, &ctx, "size", 281, &ret);
if (ret < 0)
return ret;
out->svn = parse_uint32_key(css, &ctx, "svn", 0, &ret);
if (ret < 0)
return ret;
out->modulus_size = parse_uint32_key(css, &ctx, "modulus_size", MAN_CSS_MOD_SIZE_2_5, &ret);
if (ret < 0)
return ret;
out->exponent_size = parse_uint32_key(css, &ctx, "exponent_size", MAN_CSS_EXP_SIZE, &ret);
if (ret < 0)
return ret;
/* hardcoded to align with meu */
out->reserved1[0] = 0xf;
out->reserved1[1] = 0x048e0000; // TODO: what is this ?
/* check everything parsed */
ret = assert_everything_parsed(css, &ctx);
if (ret < 0)
return ret;
if (verbose)
dump_css_v2_5(out);
/*
* values set in other places in code:
* - date
* - version
* - modulus
* - exponent
* - signature
*/
return 0;
}
static void dump_signed_pkg(const struct signed_pkg_info_ext *signed_pkg)
{
int i;
DUMP("\nsigned_pkg");
DUMP_PRINTABLE_BYTES("name", signed_pkg->name);
DUMP_KEY("vcn", "%d", signed_pkg->vcn);
DUMP_KEY("svn", "%d", signed_pkg->svn);
DUMP_KEY("fw_type", "%d", signed_pkg->fw_type);
DUMP_KEY("fw_sub_type", "%d", signed_pkg->fw_sub_type);
for (i = 0; i < ARRAY_SIZE(signed_pkg->bitmap); ++i)
DUMP_KEY("bitmap", "%d", signed_pkg->bitmap[i]);
for (i = 0; i < ARRAY_SIZE(signed_pkg->module); ++i) {
DUMP_PRINTABLE_BYTES("meta.name", signed_pkg->module[i].name);
DUMP_KEY("meta.type", "0x%x", signed_pkg->module[i].type);
DUMP_KEY("meta.hash_algo", "0x%x", signed_pkg->module[i].hash_algo);
DUMP_KEY("meta.hash_size", "0x%x", signed_pkg->module[i].hash_size);
DUMP_KEY("meta.meta_size", "%d", signed_pkg->module[i].meta_size);
}
}
static int parse_signed_pkg(const toml_table_t *toml, struct parse_ctx *pctx,
struct image *image, bool verbose)
{
struct adsp *adsp = image->adsp;
struct signed_pkg_info_ext *out = &adsp->man_v1_8->signed_pkg;
struct signed_pkg_info_module *mod;
toml_array_t *bitmap_array;
toml_array_t *module_array;
toml_table_t *signed_pkg;
struct parse_ctx ctx;
toml_table_t *module;
toml_raw_t raw;
int64_t temp_i;
int ret;
int i;
/* look for subtable in toml, increment pctx parsed table cnt and initialize local ctx */
signed_pkg = toml_table_in(toml, "signed_pkg");
if (!signed_pkg)
return err_key_not_found("signed_pkg");
++pctx->table_cnt;
parse_ctx_init(&ctx);
/* non-configurable fields */
out->ext_type = SIGN_PKG_EXT_TYPE;