forked from tangrs/nspire-linux-loader2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fdt.c
61 lines (48 loc) · 1.6 KB
/
fdt.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
#include <libfdt.h>
#include <os.h>
#include "fdt.h"
#include "common.h"
#include "memory.h"
static int _fdt_make_node(void *fdt, int parentoffset, const char *name) {
int e = fdt_subnode_offset(fdt, parentoffset, name);
if(e != FDT_ERR_NOTFOUND)
return e;
return fdt_add_subnode(fdt, parentoffset, name);
}
int update_fdt() {
static void *fdt = 0;
if(!fdt)
fdt = malloc(FDT_SIZE_MAX + 7);
if(!fdt) {
printl("Failed to allocate memory for FDT!" NEWLINE);
return 1;
}
/* Has to be 64-bit aligned */
fdt = ALIGN(fdt, 8);
if(fdt_open_into(settings.boot_param.start, fdt, FDT_SIZE_MAX) < 0) {
printl("Failed to open FDT!" NEWLINE);
return 1;
}
int chosen = _fdt_make_node(fdt, 0, "chosen");
if(chosen < 0) {
printl("Failed to create 'chosen' node!" NEWLINE);
return 1;
}
/* If not given assume defaults from DT */
if(strlen(settings.kernel_cmdline) && fdt_setprop_string(fdt, chosen, "bootargs", settings.kernel_cmdline) < 0) {
printl("Failed to set cmdline!" NEWLINE);
return 1;
}
//UNTESTED (but doesn't hurt)
if(settings.initrd_loaded) {
unsigned initrd_end = (unsigned)settings.initrd.addr + settings.initrd.size;
if(fdt_setprop_cell(fdt, chosen, "linux,initrd-start", (unsigned)settings.initrd.addr) < 0
|| fdt_setprop_cell(fdt, chosen, "linux,initrd-end", initrd_end) < 0) {
printl("Failed to set ramdisk location!" NEWLINE);
return 1;
}
}
fdt_pack(fdt);
settings.boot_param.start = fdt;
return 0;
}