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
|
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../../kexec.h"
#include "../../kexec-syscall.h"
#include <libfdt.h>
#include "ops.h"
#include "page.h"
#include "fixup_dtb.h"
#include "kexec-ppc.h"
const char proc_dts[] = "/proc/device-tree";
#ifdef DEBUG
static void print_fdt_reserve_regions(void)
{
int i, num;
/* Print out a summary of the final reserve regions */
num = fdt_num_mem_rsv(blob_buf);
printf ("reserve regions: %d\n", num);
for (i = 0; i < num; i++) {
uint64_t offset, size;
if (fdt_get_mem_rsv(blob_buf, i, &offset, &size) == 0) {
printf("%d: offset: %llx, size: %llx\n", i, offset, size);
} else {
printf("Error retreiving reserved region\n");
}
}
}
#endif
static void fixup_nodes(char *nodes[])
{
int index = 0;
char *fname;
char *prop_name;
char *node_name;
void *node;
int len;
char *content;
off_t content_size;
int ret;
while (nodes[index]) {
len = asprintf(&fname, "%s%s", proc_dts, nodes[index]);
if (len < 0)
fatal("asprintf() failed\n");
content = slurp_file(fname, &content_size);
if (!content) {
fprintf(stderr, "Can't open %s: %s\n",
fname, strerror(errno));
exit(1);
}
prop_name = fname + len;
while (*prop_name != '/')
prop_name--;
*prop_name = '\0';
prop_name++;
node_name = fname + sizeof(proc_dts) - 1;
node = finddevice(node_name);
if (!node)
node = create_node(NULL, node_name + 1);
ret = setprop(node, prop_name, content, content_size);
if (ret < 0)
fatal("setprop of %s/%s size: %ld failed: %s\n",
node_name, prop_name, content_size,
fdt_strerror(ret));
free(content);
free(fname);
index++;
};
}
/*
* command line priority:
* - use the supplied command line
* - if none available use the command line from .dtb
* - if not available use the current command line
*/
static void fixup_cmdline(const char *cmdline)
{
void *chosen;
char *fixup_cmd_node[] = {
"/chosen/bootargs",
NULL,
};
chosen = finddevice("/chosen");
if (!cmdline) {
if (!chosen)
fixup_nodes(fixup_cmd_node);
} else {
if (!chosen)
chosen = create_node(NULL, "chosen");
setprop_str(chosen, "bootargs", cmdline);
}
return;
}
#define EXPAND_GRANULARITY 1024
static char *expand_buf(int minexpand, char *blob_buf, off_t *blob_size)
{
int size = fdt_totalsize(blob_buf);
int rc;
size = _ALIGN(size + minexpand, EXPAND_GRANULARITY);
blob_buf = realloc(blob_buf, size);
if (!blob_buf)
fatal("Couldn't find %d bytes to expand device tree\n\r", size);
rc = fdt_open_into(blob_buf, blob_buf, size);
if (rc != 0)
fatal("Couldn't expand fdt into new buffer: %s\n\r",
fdt_strerror(rc));
*blob_size = fdt_totalsize(blob_buf);
return blob_buf;
}
static void fixup_reserve_regions(struct kexec_info *info, char *blob_buf)
{
int ret, i;
int nodeoffset;
u64 val = 0;
/* If this is a KEXEC kernel we add all regions since they will
* all need to be saved */
if (info->kexec_flags & KEXEC_ON_CRASH) {
for (i = 0; i < info->nr_segments; i++) {
uint64_t address = (unsigned long)info->segment[i].mem;
uint64_t size = info->segment[i].memsz;
while ((i+1) < info->nr_segments &&
(address + size == (unsigned long)info->segment[i+1].mem)) {
size += info->segment[++i].memsz;
}
ret = fdt_add_mem_rsv(blob_buf, address, size);
if (ret) {
printf("%s: Error adding memory range to memreserve!\n",
fdt_strerror(ret));
goto out;
}
}
} else if (ramdisk || reuse_initrd) {
/* Otherwise we just add back the ramdisk and the device tree
* is already in the list */
ret = fdt_add_mem_rsv(blob_buf, ramdisk_base, ramdisk_size);
if (ret) {
printf("%s: Unable to add new reserved memory for initrd flat device tree\n",
fdt_strerror(ret));
goto out;
}
}
/* Add reserve regions for cpu-release-addr */
nodeoffset = fdt_node_offset_by_prop_value(blob_buf, -1, "device_type", "cpu", 4);
while (nodeoffset != -FDT_ERR_NOTFOUND) {
const void *buf;
int sz, ret;
u64 tmp;
buf = fdt_getprop(blob_buf, nodeoffset, "cpu-release-addr", &sz);
if (buf) {
if (sz == 4) {
tmp = *(u32 *)buf;
} else if (sz == 8) {
tmp = *(u64 *)buf;
}
/* crude check to see if last value is repeated */
if (_ALIGN_DOWN(tmp, PAGE_SIZE) != _ALIGN_DOWN(val, PAGE_SIZE)) {
val = tmp;
ret = fdt_add_mem_rsv(blob_buf, _ALIGN_DOWN(val, PAGE_SIZE), PAGE_SIZE);
if (ret)
printf("%s: Unable to add reserve for cpu-release-addr!\n",
fdt_strerror(ret));
}
}
nodeoffset = fdt_node_offset_by_prop_value(blob_buf, nodeoffset,
"device_type", "cpu", 4);
}
out: ;
#ifdef DEBUG
print_fdt_reserve_regions();
#endif
}
static void fixup_memory(struct kexec_info *info, char *blob_buf)
{
if (info->kexec_flags & KEXEC_ON_CRASH) {
int nodeoffset, len = 0;
u8 tmp[16];
const unsigned long *addrcell, *sizecell;
nodeoffset = fdt_path_offset(blob_buf, "/memory");
if (nodeoffset < 0) {
printf("Error searching for memory node!\n");
return;
}
addrcell = fdt_getprop(blob_buf, 0, "#address-cells", NULL);
/* use shifts and mask to ensure endianness */
if ((addrcell) && (*addrcell == 2)) {
tmp[0] = (crash_base >> 56) & 0xff;
tmp[1] = (crash_base >> 48) & 0xff;
tmp[2] = (crash_base >> 40) & 0xff;
tmp[3] = (crash_base >> 32) & 0xff;
tmp[4] = (crash_base >> 24) & 0xff;
tmp[5] = (crash_base >> 16) & 0xff;
tmp[6] = (crash_base >> 8) & 0xff;
tmp[7] = (crash_base ) & 0xff;
len = 8;
} else {
tmp[0] = (crash_base >> 24) & 0xff;
tmp[1] = (crash_base >> 16) & 0xff;
tmp[2] = (crash_base >> 8) & 0xff;
tmp[3] = (crash_base ) & 0xff;
len = 4;
}
sizecell = fdt_getprop(blob_buf, 0, "#size-cells", NULL);
/* use shifts and mask to ensure endianness */
if ((sizecell) && (*sizecell == 2)) {
tmp[0+len] = (crash_size >> 56) & 0xff;
tmp[1+len] = (crash_size >> 48) & 0xff;
tmp[2+len] = (crash_size >> 40) & 0xff;
tmp[3+len] = (crash_size >> 32) & 0xff;
tmp[4+len] = (crash_size >> 24) & 0xff;
tmp[5+len] = (crash_size >> 16) & 0xff;
tmp[6+len] = (crash_size >> 8) & 0xff;
tmp[7+len] = (crash_size ) & 0xff;
len += 8;
} else {
tmp[0+len] = (crash_size >> 24) & 0xff;
tmp[1+len] = (crash_size >> 16) & 0xff;
tmp[2+len] = (crash_size >> 8) & 0xff;
tmp[3+len] = (crash_size ) & 0xff;
len += 4;
}
if (fdt_setprop(blob_buf, nodeoffset, "reg", tmp, len) != 0) {
printf ("Error setting memory node!\n");
}
fdt_delprop(blob_buf, nodeoffset, "linux,usable-memory");
}
}
/* removes crashkernel nodes if they exist and we are *rebooting*
* into a crashkernel. These nodes should not exist after we
* crash and reboot into a new kernel
*/
static void fixup_crashkernel(struct kexec_info *info, char *blob_buf)
{
int nodeoffset;
nodeoffset = fdt_path_offset(blob_buf, "/chosen");
if (info->kexec_flags & KEXEC_ON_CRASH) {
if (nodeoffset < 0) {
printf("fdt_crashkernel: %s\n", fdt_strerror(nodeoffset));
return;
}
fdt_delprop(blob_buf, nodeoffset, "linux,crashkernel-base");
fdt_delprop(blob_buf, nodeoffset, "linux,crashkernel-size");
}
}
/* remove the old chosen nodes if they exist and add correct chosen
* nodes if we have an initd
*/
static void fixup_initrd(char *blob_buf)
{
int err, nodeoffset;
unsigned long tmp;
nodeoffset = fdt_path_offset(blob_buf, "/chosen");
if (nodeoffset < 0) {
printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
return;
}
fdt_delprop(blob_buf, nodeoffset, "linux,initrd-start");
fdt_delprop(blob_buf, nodeoffset, "linux,initrd-end");
if ((reuse_initrd || ramdisk) &&
((ramdisk_base != 0) && (ramdisk_size != 0))) {
tmp = ramdisk_base;
err = fdt_setprop(blob_buf, nodeoffset,
"linux,initrd-start", &tmp, sizeof(tmp));
if (err < 0) {
printf("WARNING: "
"could not set linux,initrd-start %s.\n",
fdt_strerror(err));
return;
}
tmp = ramdisk_base + ramdisk_size;
err = fdt_setprop(blob_buf, nodeoffset,
"linux,initrd-end", &tmp, sizeof(tmp));
if (err < 0) {
printf("WARNING: could not set linux,initrd-end %s.\n",
fdt_strerror(err));
return;
}
}
}
char *fixup_dtb_init(struct kexec_info *info, char *blob_buf, off_t *blob_size,
unsigned long hole_addr, unsigned long *dtb_addr)
{
int ret, i, num = fdt_num_mem_rsv(blob_buf);
fdt_init(blob_buf);
/* Remove the existing reserve regions as they will no longer
* be valid after we reboot */
for (i = num - 1; i >= 0; i--) {
ret = fdt_del_mem_rsv(blob_buf, i);
if (ret) {
printf("%s: Error deleting memory reserve region %d from device tree!\n",
fdt_strerror(ret), i);
}
}
/* Pack the FDT first, so we don't grow excessively if there is already free space */
ret = fdt_pack(blob_buf);
if (ret)
printf("%s: Unable to pack flat device tree\n", fdt_strerror(ret));
/* info->nr_segments just a guide, will grow by at least EXPAND_GRANULARITY */
blob_buf = expand_buf(info->nr_segments * sizeof(struct fdt_reserve_entry),
blob_buf, blob_size);
/* add reserve region for *THIS* fdt */
*dtb_addr = locate_hole(info, *blob_size, 0,
hole_addr, hole_addr+KERNEL_ACCESS_TOP, -1);
ret = fdt_add_mem_rsv(blob_buf, *dtb_addr, PAGE_ALIGN(*blob_size));
if (ret) {
printf("%s: Unable to add new reserved memory for the flat device tree\n",
fdt_strerror(ret));
}
return blob_buf;
}
#ifdef DEBUG
static void save_fixed_up_dtb(char *blob_buf, off_t blob_size)
{
FILE *fp;
fp = fopen("debug.dtb", "w");
if (fp) {
if ( blob_size == fwrite(blob_buf, sizeof(char), blob_size, fp)) {
printf("debug.dtb written\n");
} else {
printf("Unable to write debug.dtb\n");
}
} else {
printf("Unable to dump flat device tree to debug.dtb\n");
}
}
#endif
char *fixup_dtb_finalize(struct kexec_info *info, char *blob_buf, off_t *blob_size,
char *nodes[], char *cmdline)
{
fixup_nodes(nodes);
fixup_cmdline(cmdline);
fixup_reserve_regions(info, blob_buf);
fixup_memory(info, blob_buf);
fixup_initrd(blob_buf);
fixup_crashkernel(info, blob_buf);
blob_buf = (char *)dt_ops.finalize();
*blob_size = fdt_totalsize(blob_buf);
#ifdef DEBUG
save_fixed_up_dtb(blob_buf, *blob_size);
#endif
return blob_buf;
}
|