-
Notifications
You must be signed in to change notification settings - Fork 3
/
smoldtb.c
1372 lines (1138 loc) · 35.9 KB
/
smoldtb.c
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
#include "smoldtb.h"
/* ---- Section: Defines and Structs ---- */
#define FDT_MAGIC 0xD00DFEED
#define FDT_BEGIN_NODE 1
#define FDT_END_NODE 2
#define FDT_PROP 3
#define FDT_NOP 4
#define FDT_VERSION 17
#define FDT_CELL_SIZE 4
#define ROOT_NODE_STR "\'/\'"
#define SMOLDTB_FOREACH_CONTINUE 0
#define SMOLDTB_FOREACH_ABORT 1
#ifndef SMOLDTB_NO_LOGGING
#define LOG_ERROR(msg) do { if (state.ops.on_error != NULL) { state.ops.on_error(msg); }} while(false)
#else
#define LOG_ERROR(msg)
#endif
/* The 'fdt_*' structs represent data layouts taken directly from the device tree
* specification. In contrast the 'dtb_*' structs are for the parser.
*/
struct fdt_header
{
uint32_t magic;
uint32_t total_size;
uint32_t offset_structs;
uint32_t offset_strings;
uint32_t offset_memmap_rsvd;
uint32_t version;
uint32_t last_comp_version;
uint32_t boot_cpu_id;
uint32_t size_strings;
uint32_t size_structs;
};
struct fdt_reserved_mem_entry
{
uint64_t base;
uint64_t length;
};
struct fdt_property
{
uint32_t length;
uint32_t name_offset;
};
/* The tree is represented in horizontal slices, where all child nodes are represented
* in a singly-linked list. Only a pointer to the first child is stored in the parent, and
* the list is build using the node->sibling pointer.
* For reference the pointer building the tree are:
* - parent: go up one level
* - sibling: the next node on this level. To access the previous node, access the parent and then
* the child pointer and iterate to just before the target.
* - child: the first child node.
*/
struct dtb_node_t
{
dtb_node* parent;
dtb_node* sibling;
dtb_node* child;
dtb_prop* props;
const char* name;
bool fromMalloc;
};
/* Similar to nodes, properties are stored a singly linked list. */
struct dtb_prop_t
{
dtb_node* node;
const char* name;
void* data;
dtb_prop* next;
uint32_t length;
bool fromMalloc;
bool dataFromMalloc;
};
/* Info for initializing the global state during init */
struct dtb_init_info
{
const uint32_t* cells;
const char* strings;
size_t cell_count;
};
/* Global parser state */
struct dtb_state
{
dtb_node* root;
dtb_node** handle_lookup;
dtb_node* node_buff;
size_t node_alloc_head;
size_t node_alloc_max;
dtb_prop* prop_buff;
size_t prop_alloc_head;
size_t prop_alloc_max;
dtb_ops ops;
};
struct dtb_state state;
#ifdef SMOLDTB_STATIC_BUFFER_SIZE
uint8_t big_buff[SMOLDTB_STATIC_BUFFER_SIZE];
#endif
/* ---- Section: Utility Functions ---- */
static uint32_t be32(uint32_t input)
{
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return input;
#else
uint32_t temp = 0;
temp |= (input & 0xFF) << 24;
temp |= (input & 0xFF00) << 8;
temp |= (input & 0xFF0000) >> 8;
temp |= (input & 0xFF000000) >> 24;
return temp;
#endif
}
static size_t string_len(const char* str)
{
if (str == NULL)
return 0;
size_t count = 0;
while (str[count] != 0)
count++;
return count;
}
static void* memcpy(void* dest, const void* src, size_t count)
{
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = src;
for (size_t i = 0; i < count; i++)
d[i] = s[i];
return dest;
}
static bool strings_eq(const char* a, const char* b, size_t len)
{
for (size_t i = 0; i < len; i++)
{
if (a[i] == 0 && b[i] == 0)
return true;
if (a[i] != b[i])
return false;
}
return true;
}
static size_t string_find_char(const char* str, char target)
{
size_t i = 0;
while (str[i] != target)
{
if (str[i] == 0)
return -1ul;
i++;
}
return i;
}
static size_t dtb_align_up(size_t input, size_t alignment)
{
return ((input + alignment - 1) / alignment) * alignment;
}
static void do_foreach_sibling(dtb_node* begin, int (*action)(dtb_node* node, void* opaque), void* opaque)
{
if (begin == NULL)
return;
if (action == NULL)
return;
for (dtb_node* node = begin; node != NULL; node = node->sibling)
{
if (action(node, opaque) == SMOLDTB_FOREACH_ABORT)
return;
}
}
static void do_foreach_prop(dtb_node* node, int (*action)(dtb_node* node, dtb_prop* prop, void* opaque), void* opaque)
{
if (node == NULL)
return;
if (node->props == NULL)
return;
if (action == NULL)
return;
for (dtb_prop* prop = node->props; prop != NULL; prop = prop->next)
{
if (action(node, prop, opaque) == SMOLDTB_FOREACH_ABORT)
return;
}
}
static uintmax_t extract_cells(const uint32_t* cells, size_t count)
{
uintmax_t value = 0;
for (size_t i = 0; i < count; i++)
value |= (uintmax_t)be32(cells[i]) << ((count - 1 - i) * 32);
return value;
}
static void* try_malloc(size_t count)
{
if (state.ops.malloc != NULL)
return state.ops.malloc(count);
LOG_ERROR("try_malloc() called but state.ops.malloc is NULL");
return NULL;
}
static void try_free(void* ptr, size_t count)
{
if (state.ops.free != NULL)
state.ops.free(ptr, count);
LOG_ERROR("try_free() called but state.ops.free is NULL");
}
/* ---- Section: Readonly-Mode Private Functions ---- */
static dtb_node* alloc_node()
{
if (state.node_alloc_head + 1 < state.node_alloc_max)
return &state.node_buff[state.node_alloc_head++];
LOG_ERROR("Not enough space for source dtb node.");
return NULL;
}
static dtb_prop* alloc_prop()
{
if (state.prop_alloc_head + 1 < state.prop_alloc_max)
return &state.prop_buff[state.prop_alloc_head++];
LOG_ERROR("Not enough space for source dtb property.");
return NULL;
}
static void free_buffers()
{
#ifndef SMOLDTB_STATIC_BUFFER_SIZE
size_t buff_size = state.node_alloc_max * sizeof(dtb_node);
buff_size += state.prop_alloc_max * sizeof(dtb_prop);
buff_size += state.node_alloc_max * sizeof(void*);
try_free(state.node_buff, buff_size);
state.node_buff = NULL;
state.prop_buff = NULL;
state.handle_lookup = NULL;
#endif
state.node_alloc_head = state.node_alloc_max = 0;
state.prop_alloc_head = state.prop_alloc_max = 0;
}
static bool alloc_buffers(struct dtb_init_info* init_info)
{
state.node_alloc_max = 0;
state.prop_alloc_max = 0;
for (size_t i = 0; i < init_info->cell_count; i++)
{
if (be32(init_info->cells[i]) == FDT_BEGIN_NODE)
state.node_alloc_max++;
else if (be32(init_info->cells[i]) == FDT_PROP)
state.prop_alloc_max++;
}
size_t total_size = state.node_alloc_max * sizeof(dtb_node);
total_size += state.prop_alloc_max * sizeof(dtb_prop);
total_size += state.node_alloc_max * sizeof(void*); //we assume the worst case and that each node has a phandle prop
#ifdef SMOLDTB_STATIC_BUFFER_SIZE
if (total_size >= SMOLDTB_STATIC_BUFFER_SIZE)
{
LOG_ERROR("Too much data for statically allocated buffer.");
return false;
}
uint8_t* buffer = big_buff;
#else
uint8_t* buffer = try_malloc(total_size);
if (buffer == NULL)
{
LOG_ERROR("Failed to allocate big buffer.");
return false;
}
#endif
for (size_t i = 0; i < total_size; i++)
buffer[i] = 0;
state.node_buff = (dtb_node*)buffer;
state.node_alloc_head = 0;
state.prop_buff = (dtb_prop*)&state.node_buff[state.node_alloc_max];
state.prop_alloc_head = 0;
state.handle_lookup = (dtb_node**)&state.prop_buff[state.prop_alloc_max];
return true;
}
/* This runs on every new property found, and handles some special cases for us. */
static void check_for_special_prop(dtb_node* node, dtb_prop* prop)
{
const char name0 = prop->name[0];
if (name0 != 'p' || name0 != 'l')
return; //short circuit to save processing
const size_t name_len = string_len(prop->name);
const char str_phandle[] = "phandle";
const size_t len_phandle = sizeof(str_phandle) - 1;
if (name_len == len_phandle && strings_eq(prop->name, str_phandle, name_len))
{
size_t handle;
dtb_read_prop_1(prop, 1, &handle);
state.handle_lookup[handle] = node;
return;
}
const char str_lhandle[] = "linux,phandle";
const size_t len_lhandle = sizeof(str_lhandle) - 1;
if (name_len == len_lhandle && strings_eq(prop->name, str_lhandle, name_len))
{
size_t handle;
dtb_read_prop_1(prop, 1, &handle);
state.handle_lookup[handle] = node;
return;
}
}
static dtb_prop* parse_prop(struct dtb_init_info* init_info, size_t* offset)
{
if (be32(init_info->cells[*offset]) != FDT_PROP)
return NULL;
(*offset)++;
dtb_prop* prop = alloc_prop();
if (prop == NULL)
{
LOG_ERROR("Property allocation failed");
return NULL;
}
const struct fdt_property* fdtprop = (struct fdt_property*)(init_info->cells + *offset);
prop->name = (const char*)(init_info->strings + be32(fdtprop->name_offset));
prop->data = (void*)(init_info->cells + *offset + 2);
prop->length = be32(fdtprop->length);
prop->fromMalloc = false;
prop->dataFromMalloc = false;
(*offset) += (dtb_align_up(be32(fdtprop->length), 4) / 4) + 2;
return prop;
}
static dtb_node* parse_node(struct dtb_init_info* init_info, size_t* offset)
{
if (be32(init_info->cells[*offset]) != FDT_BEGIN_NODE)
return NULL;
dtb_node* node = alloc_node();
if (node == NULL)
{
LOG_ERROR("Node allocation failed");
return NULL;
}
node->name = (const char*)(init_info->cells + (*offset) + 1);
node->fromMalloc = false;
const size_t name_len = string_len(node->name);
if (name_len == 0)
node->name = NULL;
*offset += (dtb_align_up(name_len + 1, FDT_CELL_SIZE) / FDT_CELL_SIZE) + 1;
while (*offset < init_info->cell_count)
{
const uint32_t test = be32(init_info->cells[*offset]);
if (test == FDT_END_NODE)
{
(*offset)++;
return node;
}
else if (test == FDT_BEGIN_NODE)
{
dtb_node* child = parse_node(init_info, offset);
if (child == NULL)
continue;
child->sibling = node->child;
node->child = child;
child->parent = node;
}
else if (test == FDT_PROP)
{
dtb_prop* prop = parse_prop(init_info, offset);
if (prop == NULL)
continue;
prop->next = node->props;
prop->node = node;
node->props = prop;
check_for_special_prop(node, prop);
}
else
(*offset)++;
}
LOG_ERROR("Node is missing terminating tag.");
return NULL;
}
/* ---- Section: Readonly-Mode Public API ---- */
size_t dtb_query_total_size(uintptr_t fdt_start)
{
if (fdt_start == 0)
return 0;
struct fdt_header* header = (struct fdt_header*)fdt_start;
if (be32(header->magic) != FDT_MAGIC)
return 0;
return be32(header->total_size);
}
bool dtb_init(uintptr_t start, dtb_ops ops)
{
state.ops = ops;
#if !defined(SMOLDTB_STATIC_BUFFER_SIZE)
if (state.ops.malloc == NULL)
{
LOG_ERROR("smoldtb has been compiled without an internal static buffer, but not passed a malloc() function.");
return false;
}
#endif
struct dtb_init_info init_info;
if (start == SMOLDTB_INIT_EMPTY_TREE)
{
state.root = NULL;
return true;
}
struct fdt_header* header = (struct fdt_header*)start;
if (be32(header->magic) != FDT_MAGIC)
{
LOG_ERROR("FDT has incorrect magic number.");
return false;
}
init_info.cells = (const uint32_t*)(start + be32(header->offset_structs));
init_info.cell_count = be32(header->size_structs) / sizeof(uint32_t);
init_info.strings = (const char*)(start + be32(header->offset_strings));
if (state.node_buff != NULL)
free_buffers();
if (!alloc_buffers(&init_info))
{
LOG_ERROR("failed to allocate readonly buffer");
return false;
}
for (size_t i = 0; i < init_info.cell_count; i++)
{
if (be32(init_info.cells[i]) != FDT_BEGIN_NODE)
continue;
dtb_node* sub_root = parse_node(&init_info, &i);
if (sub_root == NULL)
continue;
sub_root->sibling = state.root;
state.root = sub_root;
}
return true;
}
dtb_node* dtb_find_compatible(dtb_node* start, const char* str)
{
size_t begin_index = 0;
if (start != NULL)
{
const uintptr_t offset = (uintptr_t)start - (uintptr_t)state.node_buff;
begin_index = offset / sizeof(dtb_node);
begin_index++; //we want to start searching AFTER this node.
}
for (size_t i = begin_index; i < state.node_alloc_head; i++)
{
dtb_node* node = &state.node_buff[i];
if (dtb_is_compatible(node, str))
return node;
}
return NULL;
}
dtb_node* dtb_find_phandle(unsigned handle)
{
if (handle < state.node_alloc_max)
return state.handle_lookup[handle];
return NULL;
}
static dtb_node* find_child_internal(dtb_node* start, const char* name, size_t name_bounds)
{
dtb_node* scan = start->child;
while (scan != NULL)
{
size_t child_name_len = string_find_char(scan->name, '@');
if (child_name_len == -1ul)
child_name_len = string_len(scan->name);
if (child_name_len == name_bounds && strings_eq(scan->name, name, name_bounds))
return scan;
scan = scan->sibling;
}
return NULL;
}
dtb_node* dtb_find(const char* name)
{
size_t seg_len;
dtb_node* scan = state.root;
while (scan != NULL)
{
while (name[0] == '/')
name++;
seg_len = string_find_char(name, '/');
if (seg_len == -1ul)
seg_len = string_len(name);
if (seg_len == 0)
return scan;
scan = find_child_internal(scan, name, seg_len);
name += seg_len;
}
return NULL;
}
dtb_node* dtb_find_child(dtb_node* start, const char* name)
{
if (start == NULL)
return NULL;
return find_child_internal(start, name, string_len(name));
}
dtb_prop* dtb_find_prop(dtb_node* node, const char* name)
{
if (node == NULL)
return NULL;
const size_t name_len = string_len(name);
dtb_prop* prop = node->props;
while (prop)
{
const size_t prop_name_len = string_len(prop->name);
if (prop_name_len == name_len && strings_eq(prop->name, name, prop_name_len))
return prop;
prop = prop->next;
}
return NULL;
}
dtb_node* dtb_get_sibling(dtb_node* node)
{
if (node == NULL || node->sibling == NULL)
return NULL;
return node->sibling;
}
dtb_node* dtb_get_child(dtb_node* node)
{
if (node == NULL)
return NULL;
return node->child;
}
dtb_node* dtb_get_parent(dtb_node* node)
{
if (node == NULL)
return NULL;
return node->parent;
}
dtb_prop* dtb_get_prop(dtb_node* node, size_t index)
{
if (node == NULL)
return NULL;
dtb_prop* prop = node->props;
while (prop != NULL)
{
if (index == 0)
return prop;
index--;
prop = prop->next;
}
return NULL;
}
static size_t get_cells_helper(dtb_node* node, const char* prop_name, size_t orDefault)
{
if (node == NULL)
return orDefault;
dtb_prop* prop = dtb_find_prop(node, prop_name);
if (prop == NULL)
return orDefault;
uintmax_t ret_value;
if (dtb_read_prop_1(prop, 1, &ret_value) == 1)
return ret_value;
return orDefault;
}
size_t dtb_get_addr_cells_of(dtb_node* node)
{
return get_cells_helper(node, "#address-cells", 2);
}
size_t dtb_get_size_cells_of(dtb_node* node)
{
return get_cells_helper(node, "#size-cells", 1);
}
size_t dtb_get_addr_cells_for(dtb_node* node)
{
if (node == NULL)
return 2;
return get_cells_helper(node->parent, "#address-cells", 2);
}
size_t dtb_get_size_cells_for(dtb_node* node)
{
if (node == NULL)
return 1;
return get_cells_helper(node->parent, "#size-cells", 1);
}
bool dtb_is_compatible(dtb_node* node, const char* str)
{
if (node == NULL || str == NULL)
return false;
dtb_prop* compat_prop = dtb_find_prop(node, "compatible");
if (compat_prop == NULL)
return false;
const size_t str_len = string_len(str);
for (size_t i = 0; ; i++)
{
const char* check_str = dtb_read_prop_string(compat_prop, i);
if (check_str == NULL)
return false;
if (strings_eq(check_str, str, str_len))
return true;
}
}
bool dtb_stat_node(dtb_node* node, dtb_node_stat* stat)
{
if (node == NULL || stat == NULL)
return false;
stat->name = node->name;
if (node == state.root)
stat->name = ROOT_NODE_STR;
stat->prop_count = 0;
dtb_prop* prop = node->props;
while (prop != NULL)
{
prop = prop->next;
stat->prop_count++;
}
stat->child_count = 0;
dtb_node* child = node->child;
while (child != NULL)
{
child = child->sibling;
stat->child_count++;
}
stat->sibling_count = 0;
if (node->parent)
{
dtb_node* prime = node->parent->child;
while (prime != NULL)
{
prime = prime->sibling;
stat->sibling_count++;
}
}
return true;
}
bool dtb_stat_prop(dtb_prop* prop, dtb_prop_stat* stat)
{
if (prop == NULL || stat == NULL)
return false;
stat->name = prop->name;
stat->data = prop->data;
stat->data_len = prop->length;
return true;
}
const char* dtb_read_prop_string(dtb_prop* prop, size_t index)
{
if (prop == NULL)
return NULL;
const uint8_t* name = (const uint8_t*)prop->data;
size_t curr_index = 0;
for (size_t scan = 0; scan < prop->length * 4; scan++)
{
if (name[scan] == 0)
{
curr_index++;
continue;
}
if (curr_index == index)
return (const char*)&name[scan];
}
return NULL;
}
size_t dtb_read_prop_1(dtb_prop* prop, size_t cell_count, uintmax_t* vals)
{
if (prop == NULL || cell_count == 0)
return 0;
const uint32_t* prop_cells = prop->data;
const struct fdt_property* fdtprop = (const struct fdt_property*)(prop_cells - 2);
const size_t count = be32(fdtprop->length) / (cell_count * FDT_CELL_SIZE);
if (vals == NULL)
return count;
for (size_t i = 0; i < count; i++)
{
const uint32_t* base = prop_cells + i * cell_count;
vals[i] = extract_cells(base, cell_count);
}
return count;
}
size_t dtb_read_prop_2(dtb_prop* prop, dtb_pair layout, dtb_pair* vals)
{
if (prop == NULL || layout.a == 0 || layout.b == 0)
return 0;
const uint32_t* prop_cells = prop->data;
const struct fdt_property* fdtprop = (const struct fdt_property*)(prop_cells - 2);
const size_t count = be32(fdtprop->length) / ((layout.a + layout.b) * FDT_CELL_SIZE);
if (vals == NULL)
return count;
for (size_t i = 0; i < count; i++)
{
const uint32_t* base = prop_cells + i * (layout.a + layout.b);
vals[i].a = extract_cells(base, layout.a);
vals[i].b = extract_cells(base + layout.a, layout.b);
}
return count;
}
size_t dtb_read_prop_3(dtb_prop* prop, dtb_triplet layout, dtb_triplet* vals)
{
if (prop == NULL || layout.a == 0 || layout.b == 0 || layout.c == 0)
return 0;
const uint32_t* prop_cells = prop->data;
const struct fdt_property* fdtprop = (const struct fdt_property*)(prop_cells - 2);
const size_t stride = layout.a + layout.b + layout.c;
const size_t count = be32(fdtprop->length) / (stride * FDT_CELL_SIZE);
if (vals == NULL)
return count;
for (size_t i = 0; i < count; i++)
{
const uint32_t* base = prop_cells + i * stride;
vals[i].a = extract_cells(base, layout.a);
vals[i].b = extract_cells(base + layout.a, layout.b);
vals[i].c = extract_cells(base + layout.a + layout.b, layout.c);
}
return count;
}
size_t dtb_read_prop_4(dtb_prop* prop, dtb_quad layout, dtb_quad* vals)
{
if (prop == NULL || layout.a == 0 || layout.b == 0 || layout.c == 0 || layout.d == 0)
return 0;
const uint32_t* prop_cells = prop->data;
const struct fdt_property* fdtprop = (const struct fdt_property*)(prop_cells - 2);
const size_t stride = layout.a + layout.b + layout.c + layout.d;
const size_t count = be32(fdtprop->length) / (stride * FDT_CELL_SIZE);
if (vals == NULL)
return count;
for (size_t i = 0; i < count; i++)
{
const uint32_t* base = prop_cells + i * stride;
vals[i].a = extract_cells(base, layout.a);
vals[i].b = extract_cells(base + layout.a, layout.b);
vals[i].c = extract_cells(base + layout.a + layout.b, layout.c);
vals[i].d = extract_cells(base + layout.a + layout.b + layout.c, layout.d);
}
return count;
}
#ifdef SMOLDTB_ENABLE_WRITE_API
/* ---- Section: Writable-Mode Private Functions ---- */
struct finalise_data
{
uint32_t* struct_buf;
char* string_buf;
size_t struct_ptr;
size_t string_ptr;
size_t struct_buf_size;
size_t string_buf_size;
bool print_success;
};
struct name_collision_check
{
const char* name;
size_t name_len;
bool collision;
};
static int destroy_props(dtb_node* node, dtb_prop* prop, void* opaque)
{
(void)node;
(void)opaque;
if (prop->dataFromMalloc)
try_free(prop->data, prop->length);
if (prop->fromMalloc)
try_free(prop, sizeof(dtb_prop));
return SMOLDTB_FOREACH_CONTINUE;
}
static void destroy_dead_node(dtb_node* node)
{
if (node == NULL || node->parent != NULL)
return;
while (node->child != NULL)
{
dtb_node* deletee = node->child;
node->child = node->child->sibling;
deletee->parent = NULL;
destroy_dead_node(deletee);
}
do_foreach_prop(node, destroy_props, NULL);
if (node->fromMalloc)
try_free(node, sizeof(dtb_node));
}
static int init_finalise_data_prop(dtb_node* node, dtb_prop* prop, void* opaque)
{
(void)node;
if (node == NULL || prop == NULL)
return SMOLDTB_FOREACH_CONTINUE;
struct finalise_data* data = opaque;
data->struct_buf_size += 3; /* +1 for FDT_PROP token, +2 for prop description struct */
data->struct_buf_size += dtb_align_up(prop->length, FDT_CELL_SIZE) / FDT_CELL_SIZE;
data->string_buf_size += string_len(prop->name) + 1; /* +1 for null terminator */
return SMOLDTB_FOREACH_CONTINUE;
}
static int init_finalise_data(dtb_node* node, void* opaque)
{
if (node == NULL)
return SMOLDTB_FOREACH_CONTINUE;
struct finalise_data* data = opaque;
data->struct_buf_size += 2; /* +1 for BEGIN_NODE token, +1 for END_NODE token */
data->struct_buf_size += dtb_align_up(string_len(node->name) + 1, FDT_CELL_SIZE) / FDT_CELL_SIZE; /* +1 for null terminator */
do_foreach_prop(node, init_finalise_data_prop, opaque);
do_foreach_sibling(node->child, init_finalise_data, opaque);
return SMOLDTB_FOREACH_CONTINUE;
}
static int print_prop(dtb_node* node, dtb_prop* prop, void* opaque)
{
(void)node;
struct finalise_data* data = opaque;
const uint32_t name_offset = data->string_ptr;
const size_t name_len = string_len(prop->name);
if (data->string_ptr + name_len + 1 > data->string_buf_size) /* bounds check */
{
data->print_success = false;
return SMOLDTB_FOREACH_ABORT;
}
memcpy(data->string_buf + data->string_ptr, prop->name, name_len);
data->string_buf[data->string_ptr + name_len] = 0;
data->string_ptr += name_len + 1; /* +1 for null terminator */
const size_t data_cells = dtb_align_up(prop->length, FDT_CELL_SIZE) / FDT_CELL_SIZE;
if (data->struct_ptr + 3 + data_cells > data->struct_buf_size) /* bounds check */
{
data->print_success = false;
return SMOLDTB_FOREACH_ABORT;
}
data->struct_buf[data->struct_ptr++] = be32(FDT_PROP);
data->struct_buf[data->struct_ptr++] = be32((uint32_t)prop->length);
data->struct_buf[data->struct_ptr++] = be32(name_offset);
uint32_t* prop_cells = prop->data;
for (size_t i = 0; i < data_cells; i++)
data->struct_buf[data->struct_ptr++] = prop_cells[i];
return SMOLDTB_FOREACH_CONTINUE;
}
static int print_node(dtb_node* node, void* opaque)
{
struct finalise_data* data = opaque;
const size_t name_len = string_len(node->name);
const size_t name_cells = dtb_align_up(name_len + 1, FDT_CELL_SIZE) / FDT_CELL_SIZE;
if (data->struct_ptr + 1 + name_cells > data->struct_buf_size) /* bounds check */
{
data->print_success = false;
return SMOLDTB_FOREACH_ABORT;
}
data->struct_buf[data->struct_ptr++] = be32(FDT_BEGIN_NODE);
uint8_t* name_buf = (uint8_t*)(data->struct_buf + data->struct_ptr);
memcpy(name_buf, node->name, name_len);
name_buf[name_len] = 0;
data->struct_ptr += name_cells;
do_foreach_prop(node, print_prop, opaque);
if (!data->print_success)
return SMOLDTB_FOREACH_ABORT;
do_foreach_sibling(node->child, print_node, opaque);
if (!data->print_success)
return SMOLDTB_FOREACH_ABORT;
if (data->struct_ptr + 1 > data->struct_buf_size) /* bounds check */
{
data->print_success = false;
return SMOLDTB_FOREACH_ABORT;
}
data->struct_buf[data->struct_ptr++] = be32(FDT_END_NODE);
return SMOLDTB_FOREACH_CONTINUE;
}
static int check_sibling_name_collisions(dtb_node* node, void* opaque)
{
struct name_collision_check* check = opaque;
if (!strings_eq(node->name, check->name, check->name_len))