forked from AsahiLinux/m1n1
-
Notifications
You must be signed in to change notification settings - Fork 6
/
kboot.c
2372 lines (1906 loc) · 76 KB
/
kboot.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
/* SPDX-License-Identifier: MIT */
#include <stdint.h>
#include "kboot.h"
#include "adt.h"
#include "assert.h"
#include "clk.h"
#include "dapf.h"
#include "devicetree.h"
#include "display.h"
#include "exception.h"
#include "firmware.h"
#include "isp.h"
#include "malloc.h"
#include "mcc.h"
#include "memory.h"
#include "pcie.h"
#include "pmgr.h"
#include "sep.h"
#include "sio.h"
#include "smp.h"
#include "tunables.h"
#include "types.h"
#include "usb.h"
#include "utils.h"
#include "xnuboot.h"
#include "libfdt/libfdt.h"
#define MAX_CHOSEN_PARAMS 16
#define MAX_ATC_DEVS 8
#define MAX_CIO_DEVS 8
#define MAX_DISP_MAPPINGS 8
static void *dt = NULL;
static int dt_bufsize = 0;
static void *initrd_start = NULL;
static size_t initrd_size = 0;
static char *chosen_params[MAX_CHOSEN_PARAMS][2];
extern const char *const m1n1_version;
int dt_set_gpu(void *dt);
#define DT_ALIGN 16384
#define bail(...) \
do { \
printf(__VA_ARGS__); \
return -1; \
} while (0)
#define bail_cleanup(...) \
do { \
printf(__VA_ARGS__); \
ret = -1; \
goto err; \
} while (0)
void get_notchless_fb(u64 *fb_base, u64 *fb_height)
{
*fb_base = cur_boot_args.video.base;
*fb_height = cur_boot_args.video.height;
int node = adt_path_offset(adt, "/product");
if (node < 0) {
printf("FDT: /product node not found\n");
return;
}
u32 val;
if (ADT_GETPROP(adt, node, "partially-occluded-display", &val) < 0 || !val) {
printf("FDT: No notch detected\n");
return;
}
u64 hfrac = cur_boot_args.video.height * 16 / cur_boot_args.video.width;
u64 new_height = cur_boot_args.video.width * hfrac / 16;
if (new_height == cur_boot_args.video.height) {
printf("FDT: Notch detected, but display aspect is already 16:%lu?\n", hfrac);
return;
}
u64 offset = cur_boot_args.video.height - new_height;
printf("display: Hiding notch, %lux%lu -> %lux%lu (+%lu, 16:%lu)\n", cur_boot_args.video.width,
cur_boot_args.video.height, cur_boot_args.video.width, new_height, offset, hfrac);
*fb_base += cur_boot_args.video.stride * offset;
*fb_height = new_height;
}
static int dt_set_rng_seed_sep(int node)
{
u64 kaslr_seed;
uint8_t rng_seed[128]; // same size used by Linux for kexec
if (sep_get_random(&kaslr_seed, sizeof(kaslr_seed)) != sizeof(kaslr_seed))
bail("SEP: couldn't get enough random bytes for KASLR seed\n");
if (sep_get_random(rng_seed, sizeof(rng_seed)) != sizeof(rng_seed))
bail("SEP: couldn't get enough random bytes for RNG seed\n");
if (fdt_setprop_u64(dt, node, "kaslr-seed", kaslr_seed))
bail("FDT: couldn't set kaslr-seed\n");
if (fdt_setprop(dt, node, "rng-seed", rng_seed, sizeof(rng_seed)))
bail("FDT: couldn't set rng-seed\n");
printf("FDT: Passing %ld bytes of KASLR seed and %ld bytes of random seed\n",
sizeof(kaslr_seed), sizeof(rng_seed));
return 0;
}
static int dt_set_rng_seed_adt(int node)
{
int anode = adt_path_offset(adt, "/chosen");
if (anode < 0)
bail("ADT: /chosen not found\n");
const uint8_t *random_seed;
u32 seed_length;
random_seed = adt_getprop(adt, anode, "random-seed", &seed_length);
if (random_seed) {
printf("ADT: %d bytes of random seed available\n", seed_length);
if (seed_length >= sizeof(u64)) {
u64 kaslr_seed;
memcpy(&kaslr_seed, random_seed, sizeof(kaslr_seed));
// Ideally we would throw away the kaslr_seed part of random_seed
// and avoid reusing it. However, Linux wants 64 bytes of bootloader
// random seed to consider its CRNG initialized, which is exactly
// how much iBoot gives us. This probably doesn't matter, since
// that entropy is going to get shuffled together and Linux makes
// sure to clear the FDT randomness after using it anyway, but just
// in case let's mix in a few bits from our own KASLR base to make
// kaslr_seed unique.
kaslr_seed ^= (u64)cur_boot_args.virt_base;
if (fdt_setprop_u64(dt, node, "kaslr-seed", kaslr_seed))
bail("FDT: couldn't set kaslr-seed\n");
printf("FDT: KASLR seed initialized\n");
} else {
printf("ADT: not enough random data for kaslr-seed\n");
}
if (seed_length) {
if (fdt_setprop(dt, node, "rng-seed", random_seed, seed_length))
bail("FDT: couldn't set rng-seed\n");
printf("FDT: Passing %d bytes of random seed\n", seed_length);
}
} else {
printf("ADT: no random-seed available!\n");
}
return 0;
}
static int dt_set_chosen(void)
{
int node = fdt_path_offset(dt, "/chosen");
if (node < 0)
bail("FDT: /chosen node not found in devtree\n");
for (int i = 0; i < MAX_CHOSEN_PARAMS; i++) {
if (!chosen_params[i][0])
break;
const char *name = chosen_params[i][0];
const char *value = chosen_params[i][1];
if (fdt_setprop(dt, node, name, value, strlen(value) + 1) < 0)
bail("FDT: couldn't set chosen.%s property\n", name);
printf("FDT: %s = '%s'\n", name, value);
}
if (initrd_start && initrd_size) {
if (fdt_setprop_u64(dt, node, "linux,initrd-start", (u64)initrd_start))
bail("FDT: couldn't set chosen.linux,initrd-start property\n");
u64 end = ((u64)initrd_start) + initrd_size;
if (fdt_setprop_u64(dt, node, "linux,initrd-end", end))
bail("FDT: couldn't set chosen.linux,initrd-end property\n");
if (fdt_add_mem_rsv(dt, (u64)initrd_start, initrd_size))
bail("FDT: couldn't add reservation for the initrd\n");
printf("FDT: initrd at %p size 0x%lx\n", initrd_start, initrd_size);
}
if (cur_boot_args.video.base) {
int fb = fdt_path_offset(dt, "/chosen/framebuffer");
if (fb < 0)
bail("FDT: /chosen node not found in devtree\n");
u64 fb_base, fb_height;
get_notchless_fb(&fb_base, &fb_height);
u64 fb_size = cur_boot_args.video.stride * fb_height;
u64 fbreg[2] = {cpu_to_fdt64(fb_base), cpu_to_fdt64(fb_size)};
char fbname[32];
snprintf(fbname, sizeof(fbname), "framebuffer@%lx", fb_base);
if (fdt_setprop(dt, fb, "reg", fbreg, sizeof(fbreg)))
bail("FDT: couldn't set framebuffer.reg property\n");
if (fdt_set_name(dt, fb, fbname))
bail("FDT: couldn't set framebuffer name\n");
if (fdt_setprop_u32(dt, fb, "width", cur_boot_args.video.width))
bail("FDT: couldn't set framebuffer width\n");
if (fdt_setprop_u32(dt, fb, "height", fb_height))
bail("FDT: couldn't set framebuffer height\n");
if (fdt_setprop_u32(dt, fb, "stride", cur_boot_args.video.stride))
bail("FDT: couldn't set framebuffer stride\n");
const char *format = NULL;
switch (cur_boot_args.video.depth & 0xff) {
case 32:
format = "x8r8g8b8";
break;
case 30:
format = "x2r10g10b10";
break;
case 16:
format = "r5g6b5";
break;
default:
printf("FDT: unsupported fb depth %lu, not enabling\n", cur_boot_args.video.depth);
return 0; // Do not error out, but don't set the FB
}
if (fdt_setprop_string(dt, fb, "format", format))
bail("FDT: couldn't set framebuffer format\n");
fdt_delprop(dt, fb, "status"); // may fail if it does not exist
printf("FDT: %s base 0x%lx size 0x%lx\n", fbname, fb_base, fb_size);
// We do not need to reserve the framebuffer, as it will be excluded from the usable RAM
// range already.
// save notch height in the dcp node if present
if (cur_boot_args.video.height - fb_height) {
int dcp = fdt_path_offset(dt, "dcp");
if (dcp >= 0)
if (fdt_appendprop_u32(dt, dcp, "apple,notch-height",
cur_boot_args.video.height - fb_height))
printf("FDT: couldn't set apple,notch-height\n");
}
}
node = fdt_path_offset(dt, "/chosen");
if (node < 0)
bail("FDT: /chosen node not found in devtree\n");
if (fdt_setprop(dt, node, "asahi,iboot1-version", system_firmware.iboot,
strlen(system_firmware.iboot) + 1))
bail("FDT: couldn't set asahi,iboot1-version\n");
if (fdt_setprop(dt, node, "asahi,system-fw-version", system_firmware.string,
strlen(system_firmware.string) + 1))
bail("FDT: couldn't set asahi,system-fw-version\n");
if (fdt_setprop(dt, node, "asahi,iboot2-version", os_firmware.iboot,
strlen(os_firmware.iboot) + 1))
bail("FDT: couldn't set asahi,iboot2-version\n");
if (fdt_setprop(dt, node, "asahi,os-fw-version", os_firmware.string,
strlen(os_firmware.string) + 1))
bail("FDT: couldn't set asahi,os-fw-version\n");
if (fdt_setprop(dt, node, "asahi,m1n1-stage2-version", m1n1_version, strlen(m1n1_version) + 1))
bail("FDT: couldn't set asahi,m1n1-stage2-version\n");
if (dt_set_rng_seed_sep(node))
return dt_set_rng_seed_adt(node);
return 0;
}
static int dt_set_memory(void)
{
int anode = adt_path_offset(adt, "/chosen");
if (anode < 0)
bail("ADT: /chosen not found\n");
u64 dram_base, dram_size;
if (ADT_GETPROP(adt, anode, "dram-base", &dram_base) < 0)
bail("ADT: Failed to get dram-base\n");
if (ADT_GETPROP(adt, anode, "dram-size", &dram_size) < 0)
bail("ADT: Failed to get dram-size\n");
// Tell the kernel our usable memory range. We cannot declare all of DRAM, and just reserve the
// bottom and top, because the kernel would still map it (and just not use it), which breaks
// ioremap (e.g. simplefb).
u64 dram_min = cur_boot_args.phys_base;
u64 dram_max = cur_boot_args.phys_base + cur_boot_args.mem_size;
printf("FDT: DRAM at 0x%lx size 0x%lx\n", dram_base, dram_size);
printf("FDT: Usable memory is 0x%lx..0x%lx (0x%lx)\n", dram_min, dram_max, dram_max - dram_min);
u64 memreg[2] = {cpu_to_fdt64(dram_min), cpu_to_fdt64(dram_max - dram_min)};
int node = fdt_path_offset(dt, "/memory");
if (node < 0)
bail("FDT: /memory node not found in devtree\n");
if (fdt_setprop(dt, node, "reg", memreg, sizeof(memreg)))
bail("FDT: couldn't set memory.reg property\n");
return 0;
}
static int dt_set_serial_number(void)
{
int fdt_root = fdt_path_offset(dt, "/");
int adt_root = adt_path_offset(adt, "/");
if (fdt_root < 0)
bail("FDT: could not open a handle to FDT root.\n");
if (adt_root < 0)
bail("ADT: could not open a handle to ADT root.\n");
u32 sn_len;
const char *serial_number = adt_getprop(adt, adt_root, "serial-number", &sn_len);
if (fdt_setprop_string(dt, fdt_root, "serial-number", serial_number))
bail("FDT: unable to set device serial number!\n");
printf("FDT: reporting device serial number: %s\n", serial_number);
return 0;
}
static int dt_set_cpus(void)
{
int ret = 0;
int cpus = fdt_path_offset(dt, "/cpus");
if (cpus < 0)
bail("FDT: /cpus node not found in devtree\n");
uint32_t *pruned_phandles = calloc(MAX_CPUS, sizeof(uint32_t));
size_t pruned = 0;
if (!pruned_phandles)
bail("FDT: out of memory\n");
/* Prune CPU nodes */
int node, cpu = 0;
for (node = fdt_first_subnode(dt, cpus); node >= 0;) {
const char *name = fdt_get_name(dt, node, NULL);
if (strncmp(name, "cpu@", 4))
goto next_node;
if (cpu > MAX_CPUS)
bail_cleanup("Maximum number of CPUs exceeded, consider increasing MAX_CPUS\n");
const fdt64_t *prop = fdt_getprop(dt, node, "reg", NULL);
if (!prop)
bail_cleanup("FDT: failed to get reg property of CPU\n");
u64 dt_mpidr = fdt64_ld(prop);
if (dt_mpidr == (mrs(MPIDR_EL1) & 0xFFFFFF))
goto next_cpu;
if (!smp_is_alive(cpu)) {
printf("FDT: CPU %d is not alive, disabling...\n", cpu);
pruned_phandles[pruned++] = fdt_get_phandle(dt, node);
int next = fdt_next_subnode(dt, node);
fdt_nop_node(dt, node);
cpu++;
node = next;
continue;
}
u64 mpidr = smp_get_mpidr(cpu);
if (dt_mpidr != mpidr)
bail_cleanup("FDT: DT CPU %d MPIDR mismatch: 0x%lx != 0x%lx\n", cpu, dt_mpidr, mpidr);
u64 release_addr = smp_get_release_addr(cpu);
if (fdt_setprop_inplace_u64(dt, node, "cpu-release-addr", release_addr))
bail_cleanup("FDT: couldn't set cpu-release-addr property\n");
printf("FDT: CPU %d MPIDR=0x%lx release-addr=0x%lx\n", cpu, mpidr, release_addr);
next_cpu:
cpu++;
next_node:
node = fdt_next_subnode(dt, node);
}
if ((node < 0) && (node != -FDT_ERR_NOTFOUND)) {
bail_cleanup("FDT: error iterating through CPUs\n");
}
/* Prune AIC PMU affinities */
int aic = fdt_node_offset_by_compatible(dt, -1, "apple,aic");
if (aic == -FDT_ERR_NOTFOUND)
aic = fdt_node_offset_by_compatible(dt, -1, "apple,aic2");
if (aic < 0)
bail_cleanup("FDT: Failed to find AIC node\n");
int affinities = fdt_subnode_offset(dt, aic, "affinities");
if (affinities < 0) {
printf("FDT: Failed to find AIC affinities node, ignoring...\n");
} else {
int node;
for (node = fdt_first_subnode(dt, affinities); node >= 0;
node = fdt_next_subnode(dt, node)) {
int len;
const fdt32_t *phs = fdt_getprop(dt, node, "cpus", &len);
if (!phs)
bail_cleanup("FDT: Failed to find cpus property under AIC affinity\n");
fdt32_t *new_phs = calloc(len, 1);
size_t index = 0;
size_t count = len / sizeof(fdt32_t);
for (size_t i = 0; i < count; i++) {
uint32_t phandle = fdt32_ld(&phs[i]);
bool prune = false;
for (size_t j = 0; j < pruned; j++) {
if (pruned_phandles[j] == phandle) {
prune = true;
break;
}
}
if (!prune)
new_phs[index++] = phs[i];
}
ret = fdt_setprop(dt, node, "cpus", new_phs, sizeof(fdt32_t) * index);
free(new_phs);
if (ret < 0)
bail_cleanup("FDT: Failed to set cpus property under AIC affinity\n");
const char *name = fdt_get_name(dt, node, NULL);
printf("FDT: Pruned %ld/%ld CPU references in [AIC]/affinities/%s\n", count - index,
count, name);
}
if ((node < 0) && (node != -FDT_ERR_NOTFOUND))
bail_cleanup("FDT: Error iterating through affinity nodes\n");
}
/* Prune CPU-map */
int cpu_map = fdt_path_offset(dt, "/cpus/cpu-map");
if (cpu_map < 0) {
printf("FDT: /cpus/cpu-map node not found in devtree, ignoring...\n");
free(pruned_phandles);
return 0;
}
int cluster_idx = 0;
int cluster_node;
for (cluster_node = fdt_first_subnode(dt, cpu_map); cluster_node >= 0;) {
const char *name = fdt_get_name(dt, cluster_node, NULL);
int cpu_idx = 0;
if (strncmp(name, "cluster", 7))
goto next_cluster;
int cpu_node;
for (cpu_node = fdt_first_subnode(dt, cluster_node); cpu_node >= 0;) {
const char *cpu_name = fdt_get_name(dt, cpu_node, NULL);
if (strncmp(cpu_name, "core", 4))
goto next_map_cpu;
int len;
const fdt32_t *cpu_ph = fdt_getprop(dt, cpu_node, "cpu", &len);
if (!cpu_ph || len != sizeof(*cpu_ph))
bail_cleanup("FDT: Failed to get cpu prop for /cpus/cpu-map/%s/%s\n", name,
cpu_name);
uint32_t phandle = fdt32_ld(cpu_ph);
bool prune = false;
for (size_t i = 0; i < pruned; i++) {
if (pruned_phandles[i] == phandle) {
prune = true;
break;
}
}
if (prune) {
printf("FDT: Pruning /cpus/cpu-map/%s/%s\n", name, cpu_name);
int next = fdt_next_subnode(dt, cpu_node);
fdt_nop_node(dt, cpu_node);
cpu_node = next;
continue;
} else {
char new_name[16];
snprintf(new_name, 16, "core%d", cpu_idx++);
fdt_set_name(dt, cpu_node, new_name);
}
next_map_cpu:
cpu_node = fdt_next_subnode(dt, cpu_node);
}
if ((cpu_node < 0) && (cpu_node != -FDT_ERR_NOTFOUND))
bail_cleanup("FDT: Error iterating through CPU nodes\n");
if (cpu_idx == 0) {
printf("FDT: Pruning /cpus/cpu-map/%s\n", name);
int next = fdt_next_subnode(dt, cluster_node);
fdt_nop_node(dt, cluster_node);
cluster_node = next;
continue;
} else {
char new_name[16];
snprintf(new_name, 16, "cluster%d", cluster_idx++);
fdt_set_name(dt, cluster_node, new_name);
}
next_cluster:
cluster_node = fdt_next_subnode(dt, cluster_node);
}
if ((cluster_node < 0) && (cluster_node != -FDT_ERR_NOTFOUND))
bail_cleanup("FDT: Error iterating through CPU clusters\n");
return 0;
err:
free(pruned_phandles);
return ret;
}
static struct {
const char *alias;
const char *fdt_property;
bool swap;
} mac_address_devices[] = {
{
.alias = "bluetooth0",
.fdt_property = "local-bd-address",
.swap = true,
},
{
.alias = "ethernet0",
.fdt_property = "local-mac-address",
},
{
.alias = "wifi0",
.fdt_property = "local-mac-address",
},
};
static int dt_set_mac_addresses(void)
{
int anode = adt_path_offset(adt, "/chosen");
if (anode < 0)
bail("ADT: /chosen not found\n");
for (size_t i = 0; i < sizeof(mac_address_devices) / sizeof(*mac_address_devices); i++) {
char propname[32];
snprintf(propname, sizeof(propname), "mac-address-%s", mac_address_devices[i].alias);
uint8_t addr[6];
if (ADT_GETPROP_ARRAY(adt, anode, propname, addr) < 0)
continue;
if (mac_address_devices[i].swap) {
for (size_t i = 0; i < sizeof(addr) / 2; ++i) {
uint8_t tmp = addr[i];
addr[i] = addr[sizeof(addr) - i - 1];
addr[sizeof(addr) - i - 1] = tmp;
}
}
const char *path = fdt_get_alias(dt, mac_address_devices[i].alias);
if (path == NULL)
continue;
int node = fdt_path_offset(dt, path);
if (node < 0)
continue;
fdt_setprop(dt, node, mac_address_devices[i].fdt_property, addr, sizeof(addr));
}
return 0;
}
static int dt_set_bluetooth_cal(int anode, int node, const char *adt_name, const char *fdt_name)
{
u32 len;
const u8 *cal_blob = adt_getprop(adt, anode, adt_name, &len);
if (!cal_blob || !len)
bail("ADT: Failed to get %s\n", adt_name);
fdt_setprop(dt, node, fdt_name, cal_blob, len);
return 0;
}
static int dt_set_bluetooth(void)
{
int ret;
int anode = adt_path_offset(adt, "/arm-io/bluetooth");
if (anode < 0)
bail("ADT: /arm-io/bluetooth not found\n");
const char *path = fdt_get_alias(dt, "bluetooth0");
if (path == NULL)
return 0;
int node = fdt_path_offset(dt, path);
if (node < 0)
return 0;
ret = dt_set_bluetooth_cal(anode, node, "bluetooth-taurus-calibration-bf",
"brcm,taurus-bf-cal-blob");
if (ret)
return ret;
ret = dt_set_bluetooth_cal(anode, node, "bluetooth-taurus-calibration", "brcm,taurus-cal-blob");
if (ret)
return ret;
return 0;
}
static int dt_set_multitouch(void)
{
const char *path = fdt_get_alias(dt, "touchbar0");
if (path == NULL)
return 0;
int node = fdt_path_offset(dt, path);
if (node < 0)
bail("FDT: alias points at nonexistent node\n");
const char *adt_touchbar;
if (fdt_node_check_compatible(dt, 0, "apple,j293") == 0)
adt_touchbar = "/arm-io/spi0/multi-touch";
else if (fdt_node_check_compatible(dt, 0, "apple,j493") == 0)
adt_touchbar = "/arm-io/spi3/touch-bar";
else
return 0;
int anode = adt_path_offset(adt, adt_touchbar);
if (anode < 0)
bail("ADT: touchbar node %s not found\n", adt_touchbar);
u32 len;
const u8 *cal_blob = adt_getprop(adt, anode, "multi-touch-calibration", &len);
if (!cal_blob || !len) {
printf("ADT: Failed to get multi-touch-calibration from %s, disable %s\n", adt_touchbar,
fdt_get_name(dt, node, NULL));
fdt_setprop_string(dt, node, "status", "disabled");
return 0;
}
fdt_setprop(dt, node, "apple,z2-cal-blob", cal_blob, len);
return 0;
}
#include "keyboard_types.h"
static int dt_set_ipd(void)
{
int chosen = fdt_path_offset(dt, "/chosen");
if (chosen < 0)
bail("FDT: /chosen node not found in devtree\n");
int ipd = adt_path_offset(adt, "/arm-io/spi3/ipd");
if (ipd < 0)
ipd = adt_path_offset(adt, "/arm-io/dockchannel-mtp/mtp-transport/keyboard");
if (ipd < 0) {
printf("ADT: no keyboard found\n");
return 0;
}
u32 len;
const u8 *kblang = adt_getprop(adt, ipd, "kblang-calibration", &len);
if (!kblang || len < 2) {
printf("ADT: kblang-calibration not found, no keyboard layout\n");
return 0;
}
u8 code = kblang[1];
if (fdt_setprop_u32(dt, chosen, "asahi,kblang-code", code))
bail("FDT: couldn't set asahi,kblang-code\n");
const char *path = fdt_get_alias(dt, "keyboard");
if (path == NULL)
return 0;
int node = fdt_path_offset(dt, path);
if (node < 0)
bail("FDT: keyboard alias points at nonexistent node\n");
if (fdt_setprop_u32(dt, node, "apple,keyboard-layout-id", code))
bail("FDT: couldn't set apple,keyboard-layout-id\n");
if (code >= ARRAY_SIZE(keyboard_types))
printf("ADT: kblang code out of range, not setting country code\n");
else if (fdt_setprop_u32(dt, node, "hid-country-code", keyboard_types[code]))
bail("FDT: couldn't set hid-country-code\n");
return 0;
}
#define NVRAM_START 0x700000
#define NVRAM_MAX_SIZE SZ_1M
static int dt_set_nvram(void)
{
int adt_path[8];
u64 start, size;
int anode = adt_path_offset_trace(adt, "/arm-io/spi1/spinor/anvram", adt_path);
if (anode < 0) {
printf("ADT: nvram partition not found\n");
return 0;
}
int pp = 0;
while (adt_path[pp])
pp++;
adt_path[pp + 1] = 0;
int ret = adt_get_reg(adt, adt_path, "reg", 0, &start, &size);
if (ret < 0) {
printf("ADT: could not read nvram partition start/size\n");
return 0;
}
if (start != NVRAM_START || size > NVRAM_MAX_SIZE) {
printf("ADT: unexpected nvram partition start (0x%lx) size (0x%lx)\n", start, size);
return 0;
}
int node = fdt_path_offset(dt, "nvram");
if (node < 0) {
printf("DT: nvram alias/partition not found\n");
return 0;
}
fdt32_t reg[2];
fdt32_st(reg + 0, start);
fdt32_st(reg + 1, size);
if (fdt_setprop(dt, node, "reg", reg, sizeof(reg))) {
printf("FDT: couldn't set nvram.reg\n");
return 0;
}
if (fdt_setprop_string(dt, node, "status", "okay") < 0)
printf("FDT: failed to enable nvram partition node\n");
return 0;
}
static int dt_set_wifi(void)
{
int anode = adt_path_offset(adt, "/arm-io/wlan");
if (anode < 0)
bail("ADT: /arm-io/wlan not found\n");
uint8_t info[16];
if (ADT_GETPROP_ARRAY(adt, anode, "wifi-antenna-sku-info", info) < 0)
bail("ADT: Failed to get wifi-antenna-sku-info\n");
const char *path = fdt_get_alias(dt, "wifi0");
if (path == NULL)
return 0;
int node = fdt_path_offset(dt, path);
if (node < 0)
return 0;
char antenna[8];
memcpy(antenna, &info[8], sizeof(antenna));
fdt_setprop_string(dt, node, "apple,antenna-sku", antenna);
u32 len;
const u8 *cal_blob = adt_getprop(adt, anode, "wifi-calibration-msf", &len);
if (!cal_blob || !len)
bail("ADT: Failed to get wifi-calibration-msf\n");
fdt_setprop(dt, node, "brcm,cal-blob", cal_blob, len);
return 0;
}
static void dt_set_uboot_dm_preloc(int node)
{
// Tell U-Boot to bind this node early
fdt_setprop_empty(dt, node, "u-boot,dm-pre-reloc");
fdt_setprop_empty(dt, node, "bootph-all");
// Make sure the power domains are bound early as well
int pds_size;
const fdt32_t *pds = fdt_getprop(dt, node, "power-domains", &pds_size);
if (!pds)
return;
fdt32_t *phandles = calloc(pds_size, 1);
if (!phandles) {
printf("FDT: out of memory\n");
return;
}
memcpy(phandles, pds, pds_size);
for (int i = 0; i < pds_size / 4; i++) {
node = fdt_node_offset_by_phandle(dt, fdt32_ld(&phandles[i]));
if (node < 0)
continue;
dt_set_uboot_dm_preloc(node);
// restore node offset after DT update
node = fdt_node_offset_by_phandle(dt, fdt32_ld(&phandles[i]));
if (node < 0)
continue;
// And make sure the PMGR node is bound early too
node = fdt_parent_offset(dt, node);
if (node < 0)
continue;
dt_set_uboot_dm_preloc(node);
}
free(phandles);
}
static int dt_set_uboot(void)
{
// Make sure that U-Boot can initialize the serial port in its
// pre-relocation phase by marking its node and the nodes of the
// power domains it depends on with a "u-boot,dm-pre-reloc"
// property.
const char *path = fdt_get_alias(dt, "serial0");
if (path == NULL)
return 0;
int node = fdt_path_offset(dt, path);
if (node < 0)
return 0;
dt_set_uboot_dm_preloc(node);
return 0;
}
struct atc_tunable {
u32 offset : 24;
u32 size : 8;
u32 mask;
u32 value;
} PACKED;
static_assert(sizeof(struct atc_tunable) == 12, "Invalid atc_tunable size");
struct adt_tunable_info {
const char *adt_name;
const char *fdt_name;
size_t reg_offset;
size_t reg_size;
bool required;
};
static const struct adt_tunable_info atc_tunables[] = {
/* global tunables applied after power on or reset */
{"tunable_ATC0AXI2AF", "apple,tunable-axi2af", 0x0, 0x4000, true},
{"tunable_ATC_FABRIC", "apple,tunable-common", 0x45000, 0x4000, true},
{"tunable_AUS_CMN_TOP", "apple,tunable-common", 0x800, 0x4000, true},
{"tunable_AUS_CMN_SHM", "apple,tunable-common", 0xa00, 0x4000, true},
{"tunable_AUSPLL_CORE", "apple,tunable-common", 0x2200, 0x4000, true},
{"tunable_AUSPLL_TOP", "apple,tunable-common", 0x2000, 0x4000, true},
{"tunable_CIO3PLL_CORE", "apple,tunable-common", 0x2a00, 0x4000, true},
{"tunable_CIO3PLL_TOP", "apple,tunable-common", 0x2800, 0x4000, true},
{"tunable_CIO_CIO3PLL_TOP", "apple,tunable-common", 0x2800, 0x4000, false},
{"tunable_USB_ACIOPHY_TOP", "apple,tunable-common", 0x0, 0x4000, true},
/* lane-specific tunables applied after a cable is connected */
{"tunable_DP_LN0_AUSPMA_TX_TOP", "apple,tunable-lane0-dp", 0xc000, 0x1000, true},
{"tunable_DP_LN1_AUSPMA_TX_TOP", "apple,tunable-lane1-dp", 0x13000, 0x1000, true},
{"tunable_USB_LN0_AUSPMA_RX_TOP", "apple,tunable-lane0-usb", 0x9000, 0x1000, true},
{"tunable_USB_LN0_AUSPMA_RX_EQ", "apple,tunable-lane0-usb", 0xa000, 0x1000, true},
{"tunable_USB_LN0_AUSPMA_RX_SHM", "apple,tunable-lane0-usb", 0xb000, 0x1000, true},
{"tunable_USB_LN0_AUSPMA_TX_TOP", "apple,tunable-lane0-usb", 0xc000, 0x1000, true},
{"tunable_USB_LN1_AUSPMA_RX_TOP", "apple,tunable-lane1-usb", 0x10000, 0x1000, true},
{"tunable_USB_LN1_AUSPMA_RX_EQ", "apple,tunable-lane1-usb", 0x11000, 0x1000, true},
{"tunable_USB_LN1_AUSPMA_RX_SHM", "apple,tunable-lane1-usb", 0x12000, 0x1000, true},
{"tunable_USB_LN1_AUSPMA_TX_TOP", "apple,tunable-lane1-usb", 0x13000, 0x1000, true},
{"tunable_CIO_LN0_AUSPMA_RX_TOP", "apple,tunable-lane0-cio", 0x9000, 0x1000, true},
{"tunable_CIO_LN0_AUSPMA_RX_EQ", "apple,tunable-lane0-cio", 0xa000, 0x1000, true},
{"tunable_CIO_LN0_AUSPMA_RX_SHM", "apple,tunable-lane0-cio", 0xb000, 0x1000, true},
{"tunable_CIO_LN0_AUSPMA_TX_TOP", "apple,tunable-lane0-cio", 0xc000, 0x1000, true},
{"tunable_CIO_LN1_AUSPMA_RX_TOP", "apple,tunable-lane1-cio", 0x10000, 0x1000, true},
{"tunable_CIO_LN1_AUSPMA_RX_EQ", "apple,tunable-lane1-cio", 0x11000, 0x1000, true},
{"tunable_CIO_LN1_AUSPMA_RX_SHM", "apple,tunable-lane1-cio", 0x12000, 0x1000, true},
{"tunable_CIO_LN1_AUSPMA_TX_TOP", "apple,tunable-lane1-cio", 0x13000, 0x1000, true},
};
static int dt_append_atc_tunable(int adt_node, int fdt_node,
const struct adt_tunable_info *tunable_info)
{
u32 tunables_len;
const struct atc_tunable *tunable_adt =
adt_getprop(adt, adt_node, tunable_info->adt_name, &tunables_len);
if (!tunable_adt) {
printf("ADT: tunable %s not found\n", tunable_info->adt_name);
if (tunable_info->required)
return -1;
else
return 0;
}
if (tunables_len % sizeof(*tunable_adt)) {
printf("ADT: tunable %s with invalid length %d\n", tunable_info->adt_name, tunables_len);
return -1;
}
u32 n_tunables = tunables_len / sizeof(*tunable_adt);
for (size_t j = 0; j < n_tunables; j++) {
const struct atc_tunable *tunable = &tunable_adt[j];
if (tunable->size != 32) {
printf("kboot: ATC tunable has invalid size %d\n", tunable->size);
return -1;
}
if (tunable->offset % (tunable->size / 8)) {
printf("kboot: ATC tunable has unaligned offset %x\n", tunable->offset);
return -1;
}
if (tunable->offset + (tunable->size / 8) > tunable_info->reg_size) {
printf("kboot: ATC tunable has invalid offset %x\n", tunable->offset);
return -1;
}
if (fdt_appendprop_u32(dt, fdt_node, tunable_info->fdt_name,
tunable->offset + tunable_info->reg_offset) < 0)
return -1;
if (fdt_appendprop_u32(dt, fdt_node, tunable_info->fdt_name, tunable->mask) < 0)
return -1;
if (fdt_appendprop_u32(dt, fdt_node, tunable_info->fdt_name, tunable->value) < 0)
return -1;
}
return 0;
}
static void dt_copy_atc_tunables(const char *adt_path, const char *dt_alias)
{
int ret;
int adt_node = adt_path_offset(adt, adt_path);
if (adt_node < 0)
return;
const char *fdt_path = fdt_get_alias(dt, dt_alias);
if (fdt_path == NULL) {
printf("FDT: Unable to find alias %s\n", dt_alias);
return;
}
int fdt_node = fdt_path_offset(dt, fdt_path);
if (fdt_node < 0) {
printf("FDT: Unable to find path %s for alias %s\n", fdt_path, dt_alias);
return;
}
for (size_t i = 0; i < sizeof(atc_tunables) / sizeof(*atc_tunables); ++i) {
ret = dt_append_atc_tunable(adt_node, fdt_node, &atc_tunables[i]);