2011-04-01 12:18:40 +00:00
|
|
|
#include <boot.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <driver.h>
|
|
|
|
#include <environment.h>
|
|
|
|
#include <image.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <xfuncs.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2011-12-06 07:53:26 +00:00
|
|
|
#include <sizes.h>
|
|
|
|
#include <libbb.h>
|
2012-04-11 05:42:34 +00:00
|
|
|
#include <magicvar.h>
|
2012-04-13 10:12:25 +00:00
|
|
|
#include <libfdt.h>
|
2012-04-13 07:08:04 +00:00
|
|
|
#include <binfmt.h>
|
2011-04-01 12:18:40 +00:00
|
|
|
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <asm/setup.h>
|
|
|
|
#include <asm/barebox-arm.h>
|
|
|
|
#include <asm/armlinux.h>
|
|
|
|
#include <asm/system.h>
|
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
static int __do_bootm_linux(struct image_data *data, int swap)
|
2011-04-01 12:18:40 +00:00
|
|
|
{
|
2011-12-06 07:53:26 +00:00
|
|
|
unsigned long kernel;
|
|
|
|
unsigned long initrd_start = 0, initrd_size = 0;
|
|
|
|
struct memory_bank *bank;
|
|
|
|
unsigned long load_address;
|
|
|
|
|
|
|
|
if (data->os_res) {
|
|
|
|
load_address = data->os_res->start;
|
|
|
|
} else if (data->os_address != UIMAGE_INVALID_ADDRESS) {
|
|
|
|
load_address = data->os_address;
|
|
|
|
} else {
|
|
|
|
bank = list_first_entry(&memory_banks,
|
|
|
|
struct memory_bank, list);
|
|
|
|
load_address = bank->start + SZ_32K;
|
|
|
|
if (bootm_verbose(data))
|
|
|
|
printf("no os load address, defaulting to 0x%08lx\n",
|
|
|
|
load_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->os_res && data->os) {
|
|
|
|
data->os_res = uimage_load_to_sdram(data->os,
|
|
|
|
data->os_num, load_address);
|
|
|
|
if (!data->os_res)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->os_res) {
|
|
|
|
data->os_res = file_to_sdram(data->os_file, load_address);
|
|
|
|
if (!data->os_res)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
kernel = data->os_res->start + data->os_entry;
|
|
|
|
|
2012-08-12 18:04:23 +00:00
|
|
|
initrd_start = data->initrd_address;
|
|
|
|
|
|
|
|
if (data->initrd_file && initrd_start == UIMAGE_INVALID_ADDRESS) {
|
2011-12-06 07:53:26 +00:00
|
|
|
initrd_start = data->os_res->start + SZ_8M;
|
2011-04-01 12:18:40 +00:00
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
if (bootm_verbose(data)) {
|
|
|
|
printf("no initrd load address, defaulting to 0x%08lx\n",
|
|
|
|
initrd_start);
|
|
|
|
}
|
|
|
|
}
|
2011-04-01 12:18:40 +00:00
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
if (data->initrd) {
|
|
|
|
data->initrd_res = uimage_load_to_sdram(data->initrd,
|
|
|
|
data->initrd_num, initrd_start);
|
|
|
|
if (!data->initrd_res)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else if (data->initrd_file) {
|
|
|
|
data->initrd_res = file_to_sdram(data->initrd_file, initrd_start);
|
|
|
|
if (!data->initrd_res)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2011-04-01 12:18:40 +00:00
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
if (data->initrd_res) {
|
|
|
|
initrd_start = data->initrd_res->start;
|
2012-05-24 06:52:22 +00:00
|
|
|
initrd_size = resource_size(data->initrd_res);
|
2011-12-06 07:53:26 +00:00
|
|
|
}
|
2011-04-01 12:18:40 +00:00
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
if (bootm_verbose(data)) {
|
|
|
|
printf("\nStarting kernel at 0x%08lx", kernel);
|
|
|
|
if (initrd_size)
|
|
|
|
printf(", initrd at 0x%08lx", initrd_start);
|
|
|
|
if (data->oftree)
|
|
|
|
printf(", oftree at 0x%p", data->oftree);
|
|
|
|
printf("...\n");
|
|
|
|
}
|
2011-04-01 12:18:40 +00:00
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
start_linux((void *)kernel, swap, initrd_start, initrd_size, data->oftree);
|
|
|
|
|
|
|
|
reset_cpu(0);
|
|
|
|
|
|
|
|
return -ERESTARTSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_bootm_linux(struct image_data *data)
|
|
|
|
{
|
|
|
|
return __do_bootm_linux(data, 0);
|
2011-04-01 12:18:40 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
static struct image_handler uimage_handler = {
|
|
|
|
.name = "ARM Linux uImage",
|
2011-04-01 12:18:40 +00:00
|
|
|
.bootm = do_bootm_linux,
|
2011-12-06 07:53:26 +00:00
|
|
|
.filetype = filetype_uimage,
|
|
|
|
.ih_os = IH_OS_LINUX,
|
2011-04-01 12:18:40 +00:00
|
|
|
};
|
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
static struct image_handler rawimage_handler = {
|
|
|
|
.name = "ARM raw image",
|
|
|
|
.bootm = do_bootm_linux,
|
|
|
|
.filetype = filetype_unknown,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct zimage_header {
|
|
|
|
u32 unused[9];
|
|
|
|
u32 magic;
|
|
|
|
u32 start;
|
|
|
|
u32 end;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ZIMAGE_MAGIC 0x016F2818
|
|
|
|
|
2012-04-13 10:12:25 +00:00
|
|
|
static int do_bootz_linux_fdt(int fd, struct image_data *data)
|
|
|
|
{
|
|
|
|
struct fdt_header __header, *header;
|
|
|
|
struct resource *r = data->os_res;
|
|
|
|
struct resource *of_res = data->os_res;
|
|
|
|
void *oftree;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
u32 end;
|
|
|
|
|
|
|
|
header = &__header;
|
|
|
|
ret = read(fd, header, sizeof(*header));
|
|
|
|
if (ret < sizeof(*header))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (file_detect_type(header) != filetype_oftree)
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
end = be32_to_cpu(header->totalsize);
|
|
|
|
|
|
|
|
if (IS_BUILTIN(CONFIG_OFTREE)) {
|
|
|
|
oftree = malloc(end + 0x8000);
|
|
|
|
if (!oftree) {
|
|
|
|
perror("zImage: oftree malloc");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2012-05-24 06:52:22 +00:00
|
|
|
of_res = request_sdram_region("oftree", r->start + resource_size(r), end);
|
2012-04-13 10:12:25 +00:00
|
|
|
if (!of_res) {
|
|
|
|
perror("zImage: oftree request_sdram_region");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
oftree = (void*)of_res->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(oftree, header, sizeof(*header));
|
|
|
|
|
|
|
|
end -= sizeof(*header);
|
|
|
|
|
|
|
|
ret = read_full(fd, oftree + sizeof(*header), end);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret < end) {
|
|
|
|
printf("premature end of image\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_BUILTIN(CONFIG_OFTREE)) {
|
|
|
|
fdt_open_into(oftree, oftree, end + 0x8000);
|
|
|
|
|
|
|
|
ret = of_fix_tree(oftree);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
data->oftree = oftree;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("zImage: concatenated oftree detected\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
static int do_bootz_linux(struct image_data *data)
|
|
|
|
{
|
|
|
|
int fd, ret, swap = 0;
|
|
|
|
struct zimage_header __header, *header;
|
|
|
|
void *zimage;
|
|
|
|
u32 end;
|
|
|
|
unsigned long load_address = data->os_address;
|
|
|
|
|
|
|
|
if (load_address == UIMAGE_INVALID_ADDRESS) {
|
|
|
|
struct memory_bank *bank = list_first_entry(&memory_banks,
|
|
|
|
struct memory_bank, list);
|
|
|
|
data->os_address = bank->start + SZ_8M;
|
2011-12-21 10:33:31 +00:00
|
|
|
load_address = data->os_address;
|
2011-12-06 07:53:26 +00:00
|
|
|
if (bootm_verbose(data))
|
|
|
|
printf("no os load address, defaulting to 0x%08lx\n",
|
|
|
|
load_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(data->os_file, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = &__header;
|
|
|
|
ret = read(fd, header, sizeof(*header));
|
|
|
|
if (ret < sizeof(*header)) {
|
|
|
|
printf("could not read %s\n", data->os_file);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (header->magic) {
|
|
|
|
case swab32(ZIMAGE_MAGIC):
|
|
|
|
swap = 1;
|
|
|
|
/* fall through */
|
|
|
|
case ZIMAGE_MAGIC:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("invalid magic 0x%08x\n", header->magic);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = header->end;
|
|
|
|
|
|
|
|
if (swap)
|
|
|
|
end = swab32(end);
|
|
|
|
|
|
|
|
data->os_res = request_sdram_region("zimage", load_address, end);
|
|
|
|
if (!data->os_res) {
|
2012-04-18 12:30:02 +00:00
|
|
|
pr_err("bootm/zImage: failed to request memory at 0x%lx to 0x%lx (%d).\n",
|
|
|
|
load_address, load_address + end, end);
|
2011-12-06 07:53:26 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
zimage = (void *)data->os_res->start;
|
|
|
|
|
|
|
|
memcpy(zimage, header, sizeof(*header));
|
|
|
|
|
|
|
|
ret = read_full(fd, zimage + sizeof(*header), end - sizeof(*header));
|
|
|
|
if (ret < 0)
|
|
|
|
goto err_out;
|
|
|
|
if (ret < end - sizeof(*header)) {
|
|
|
|
printf("premature end of image\n");
|
|
|
|
ret = -EIO;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swap) {
|
|
|
|
void *ptr;
|
|
|
|
for (ptr = zimage; ptr < zimage + end; ptr += 4)
|
|
|
|
*(u32 *)ptr = swab32(*(u32 *)ptr);
|
|
|
|
}
|
|
|
|
|
2012-04-13 10:12:25 +00:00
|
|
|
ret = do_bootz_linux_fdt(fd, data);
|
|
|
|
if (ret && ret != -ENXIO)
|
|
|
|
return ret;
|
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
return __do_bootm_linux(data, swap);
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct image_handler zimage_handler = {
|
|
|
|
.name = "ARM zImage",
|
|
|
|
.bootm = do_bootz_linux,
|
|
|
|
.filetype = filetype_arm_zimage,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int do_bootm_barebox(struct image_data *data)
|
2011-04-01 12:18:40 +00:00
|
|
|
{
|
2011-12-06 07:53:26 +00:00
|
|
|
void (*barebox)(void);
|
|
|
|
|
|
|
|
barebox = read_file(data->os_file, NULL);
|
|
|
|
if (!barebox)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
shutdown_barebox();
|
|
|
|
|
|
|
|
barebox();
|
|
|
|
|
|
|
|
reset_cpu(0);
|
2011-04-01 12:18:40 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
static struct image_handler barebox_handler = {
|
|
|
|
.name = "ARM barebox",
|
|
|
|
.bootm = do_bootm_barebox,
|
|
|
|
.filetype = filetype_arm_barebox,
|
|
|
|
};
|
|
|
|
|
2012-04-11 05:42:34 +00:00
|
|
|
#include <aimage.h>
|
|
|
|
|
|
|
|
static int aimage_load_resource(int fd, struct resource *r, void* buf, int ps)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
void *image = (void *)r->start;
|
2012-05-24 06:52:22 +00:00
|
|
|
unsigned to_read = ps - resource_size(r) % ps;
|
2012-04-11 05:42:34 +00:00
|
|
|
|
2012-05-24 06:52:22 +00:00
|
|
|
ret = read_full(fd, image, resource_size(r));
|
2012-04-11 05:42:34 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = read_full(fd, buf, to_read);
|
|
|
|
if (ret < 0)
|
|
|
|
printf("could not read dummy %d\n", to_read);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_bootm_aimage(struct image_data *data)
|
|
|
|
{
|
|
|
|
struct resource *snd_stage_res;
|
|
|
|
int fd, ret;
|
|
|
|
struct android_header __header, *header;
|
|
|
|
void *buf;
|
|
|
|
int to_read;
|
|
|
|
struct android_header_comp *cmp;
|
|
|
|
|
|
|
|
fd = open(data->os_file, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = &__header;
|
|
|
|
ret = read(fd, header, sizeof(*header));
|
|
|
|
if (ret < sizeof(*header)) {
|
|
|
|
printf("could not read %s\n", data->os_file);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Android Image for '%s'\n", header->name);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As on tftp we do not support lseek and we will just have to seek
|
|
|
|
* for the size of a page - 1 max just buffer instead to read to dummy
|
|
|
|
* data
|
|
|
|
*/
|
|
|
|
buf = xmalloc(header->page_size);
|
|
|
|
|
|
|
|
to_read = header->page_size - sizeof(*header);
|
|
|
|
ret = read_full(fd, buf, to_read);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("could not read dummy %d from %s\n", to_read, data->os_file);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmp = &header->kernel;
|
|
|
|
data->os_res = request_sdram_region("akernel", cmp->load_addr, cmp->size);
|
|
|
|
if (!data->os_res) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = aimage_load_resource(fd, data->os_res, buf, header->page_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("could not read kernel");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fastboot always expect a ramdisk
|
|
|
|
* in barebox we can be less restrictive
|
|
|
|
*/
|
|
|
|
cmp = &header->ramdisk;
|
|
|
|
if (cmp->size) {
|
|
|
|
data->initrd_res = request_sdram_region("ainitrd", cmp->load_addr, cmp->size);
|
|
|
|
if (!data->initrd_res) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = aimage_load_resource(fd, data->initrd_res, buf, header->page_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("could not read initrd");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!getenv("aimage_noverwrite_bootargs"))
|
2012-04-29 18:13:53 +00:00
|
|
|
linux_bootargs_overwrite(header->cmdline);
|
2012-04-11 05:42:34 +00:00
|
|
|
|
|
|
|
if (!getenv("aimage_noverwrite_tags"))
|
|
|
|
armlinux_set_bootparams((void*)header->tags_addr);
|
|
|
|
|
2012-07-05 19:12:07 +00:00
|
|
|
if (IS_ENABLED(CONFIG_OFTREE) && data->oftree) {
|
2012-04-11 05:42:34 +00:00
|
|
|
ret = of_fix_tree(data->oftree);
|
|
|
|
if (ret)
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmp = &header->second_stage;
|
|
|
|
if (cmp->size) {
|
|
|
|
void (*second)(void);
|
|
|
|
|
|
|
|
snd_stage_res = request_sdram_region("asecond", cmp->load_addr, cmp->size);
|
|
|
|
if (!snd_stage_res) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = aimage_load_resource(fd, snd_stage_res, buf, header->page_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("could not read initrd");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
second = (void*)snd_stage_res->start;
|
|
|
|
shutdown_barebox();
|
|
|
|
|
|
|
|
second();
|
|
|
|
|
|
|
|
reset_cpu(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return __do_bootm_linux(data, 0);
|
|
|
|
|
|
|
|
err_out:
|
2012-04-29 18:13:53 +00:00
|
|
|
linux_bootargs_overwrite(NULL);
|
2012-04-11 05:42:34 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct image_handler aimage_handler = {
|
|
|
|
.name = "ARM Android Image",
|
|
|
|
.bootm = do_bootm_aimage,
|
|
|
|
.filetype = filetype_aimage,
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_CMD_BOOTM_AIMAGE
|
|
|
|
BAREBOX_MAGICVAR(aimage_noverwrite_bootargs, "Disable overwrite of the bootargs with the one present in aimage");
|
|
|
|
BAREBOX_MAGICVAR(aimage_noverwrite_tags, "Disable overwrite of the tags addr with the one present in aimage");
|
|
|
|
#endif
|
|
|
|
|
2012-04-13 07:08:04 +00:00
|
|
|
static struct binfmt_hook binfmt_aimage_hook = {
|
|
|
|
.type = filetype_aimage,
|
|
|
|
.exec = "bootm",
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct binfmt_hook binfmt_arm_zimage_hook = {
|
|
|
|
.type = filetype_arm_zimage,
|
|
|
|
.exec = "bootm",
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct binfmt_hook binfmt_barebox_hook = {
|
|
|
|
.type = filetype_arm_barebox,
|
|
|
|
.exec = "bootm",
|
|
|
|
};
|
|
|
|
|
2011-12-06 07:53:26 +00:00
|
|
|
static int armlinux_register_image_handler(void)
|
|
|
|
{
|
|
|
|
register_image_handler(&barebox_handler);
|
|
|
|
register_image_handler(&uimage_handler);
|
|
|
|
register_image_handler(&rawimage_handler);
|
|
|
|
register_image_handler(&zimage_handler);
|
2012-04-13 07:08:04 +00:00
|
|
|
if (IS_BUILTIN(CONFIG_CMD_BOOTM_AIMAGE)) {
|
2012-04-11 05:42:34 +00:00
|
|
|
register_image_handler(&aimage_handler);
|
2012-04-13 07:08:04 +00:00
|
|
|
binfmt_register(&binfmt_aimage_hook);
|
|
|
|
}
|
|
|
|
binfmt_register(&binfmt_arm_zimage_hook);
|
|
|
|
binfmt_register(&binfmt_barebox_hook);
|
2011-12-06 07:53:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-01 12:18:40 +00:00
|
|
|
late_initcall(armlinux_register_image_handler);
|