forked from tangrs/nspire-linux-loader2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
abooter.c
109 lines (86 loc) · 3.36 KB
/
abooter.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
/*
TI-NSPIRE Linux In-Place Bootloader boot.img Module
Copyright (C) 2015 Josh Max
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "memory.h"
#include "bootimg.h"
void load_bootimg(const char *filename) {
size_t bootimg_size = file_size(filename);
size_t size_free_memory = mem_block_size_free() -
(settings.kernel.size + settings.initrd.size +
sizeof(settings.kernel_cmdline) + 2048);
static unsigned char buf[ABOOT_PAGE_SIZE];
struct boot_img_hdr *hdr = (void*) buf;
unsigned int n;
FILE *f;
if (!bootimg_size) {
printl("boot.img doesn't exist or empty" NEWLINE);
return;
}
if (bootimg_size > size_free_memory) {
printl( "boot.img is too large!" NEWLINE
"Tried to load %u bytes into %u bytes of free space" NEWLINE,
bootimg_size, size_free_memory);
return;
}
f = fopen(filename, "rb");
if (!f) {
printl("Failed to open boot image %s" NEWLINE, filename);
return;
}
if (fread(buf, 1, ABOOT_PAGE_SIZE, f) != ABOOT_PAGE_SIZE) {
printl("Failed to read boot image metadata" NEWLINE);
return;
}
if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE) != 0) {
printl("Invalid boot image magic" NEWLINE);
return;
}
n = ROUND_TO_PAGE(hdr->kernel_size);
/* Load the kernel and hope we didn't hit anything important */
printl("Loading boot image kernel... ");
if (fread(settings.mem_block.start, 1, n, f) != n) {
printl("Couldn't load kernel from boot.img" NEWLINE);
return;
}
printl("Done!" NEWLINE);
settings.kernel.addr = settings.mem_block.start;
settings.kernel.size = n;
settings.kernel_loaded = 1;
n = ROUND_TO_PAGE(hdr->ramdisk_size);
printl("Loading boot image ramdisk... ");
/* Set initrd load address location */
void *initrd_laddr = ((char*) settings.mem_block.start + settings.mem_block.size - n);
initrd_laddr = ROUND_PAGE_BOUND(initrd_laddr);
size_t needed_size = ((char*) settings.mem_block.start + settings.mem_block.size)
- (char*) initrd_laddr;
if (fread(initrd_laddr, 1, n, f) != n) {
printl("Couldn't load ramdisk from boot.img" NEWLINE);
return;
}
printl("Done!" NEWLINE);
settings.initrd.addr = initrd_laddr;
settings.initrd.size = needed_size;
settings.initrd_loaded = 1;
/* Only copy the first 127 characters of cmdline */
if (hdr->cmdline[0]) {
for (n = 0; n < 127; n++)
settings.kernel_cmdline[n] = hdr->cmdline[n];
hdr->cmdline[127] = 0;
printl("Kernel cmdline: %s" NEWLINE, hdr->cmdline);
}
fclose(f);
printl("Boot image successfully loaded" NEWLINE);
return;
}