Merge branch 'master' into isa/arm64
[lunaix-os.git] / lunaix-os / tests / units / device-tree / load.c
1 #include "common.h"
2
3 #include <sys/mman.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <errno.h>
8
9 #define MAX_DTB_SZ (2 * 1024 * 1024)
10
11 #ifndef TEST_DTBFILE
12 #warning "no test dtb file given"
13 #define TEST_DTBFILE ""
14 #endif
15
16 extern void init_export___init_devtree();
17
18 ptr_t
19 load_fdt()
20 {
21     int fd = open(TEST_DTBFILE, O_RDONLY);
22     if (fd == -1) {
23         printf("fail to open: %s, %s\n", TEST_DTBFILE, strerror(errno));
24         return false;
25     }
26
27     ptr_t dtb = (ptr_t)mmap(0, 
28                             MAX_DTB_SZ, 
29                             PROT_READ | PROT_WRITE | MAP_POPULATE, 
30                             MAP_PRIVATE, fd, 0);
31     
32     if (dtb == (ptr_t)-1) {
33         printf("fail to map dtb: %s\n", strerror(errno));
34         _exit(1);
35     }
36
37     close(fd);
38     return dtb;
39 }
40
41 bool
42 my_load_dtb()
43 {
44     init_export___init_devtree();
45     return dt_load(load_fdt());
46 }