xref: /openbmc/u-boot/arch/sh/lib/bootm.c (revision fabbeb33)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (c) Copyright 2008 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
7  * (c) Copyright 2008 Renesas Solutions Corp.
8  */
9 
10 #include <common.h>
11 #include <command.h>
12 #include <asm/byteorder.h>
13 #include <asm/zimage.h>
14 
15 #ifdef CONFIG_SYS_DEBUG
16 static void hexdump(unsigned char *buf, int len)
17 {
18 	int i;
19 
20 	for (i = 0; i < len; i++) {
21 		if ((i % 16) == 0)
22 			printf("%s%08x: ", i ? "\n" : "",
23 							(unsigned int)&buf[i]);
24 		printf("%02x ", buf[i]);
25 	}
26 	printf("\n");
27 }
28 #endif
29 
30 #ifdef CONFIG_SH_SDRAM_OFFSET
31 #define GET_INITRD_START(initrd, linux) (initrd - linux + CONFIG_SH_SDRAM_OFFSET)
32 #else
33 #define GET_INITRD_START(initrd, linux) (initrd - linux)
34 #endif
35 
36 static void set_sh_linux_param(unsigned long param_addr, unsigned long data)
37 {
38 	*(unsigned long *)(param_addr) = data;
39 }
40 
41 static unsigned long sh_check_cmd_arg(char *cmdline, char *key, int base)
42 {
43 	unsigned long val = 0;
44 	char *p = strstr(cmdline, key);
45 	if (p) {
46 		p += strlen(key);
47 		val = simple_strtol(p, NULL, base);
48 	}
49 	return val;
50 }
51 
52 int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
53 {
54 	/* Linux kernel load address */
55 	void (*kernel) (void) = (void (*)(void))images->ep;
56 	/* empty_zero_page */
57 	unsigned char *param
58 		= (unsigned char *)image_get_load(images->legacy_hdr_os);
59 	/* Linux kernel command line */
60 	char *cmdline = (char *)param + COMMAND_LINE;
61 	/* PAGE_SIZE */
62 	unsigned long size = images->ep - (unsigned long)param;
63 	char *bootargs = env_get("bootargs");
64 
65 	/*
66 	 * allow the PREP bootm subcommand, it is required for bootm to work
67 	 */
68 	if (flag & BOOTM_STATE_OS_PREP)
69 		return 0;
70 
71 	if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
72 		return 1;
73 
74 	/* Clear zero page */
75 	memset(param, 0, size);
76 
77 	/* Set commandline */
78 	strcpy(cmdline, bootargs);
79 
80 	/* Initrd */
81 	if (images->rd_start || images->rd_end) {
82 		unsigned long ramdisk_flags = 0;
83 		int val = sh_check_cmd_arg(bootargs, CMD_ARG_RD_PROMPT, 10);
84 		if (val == 1)
85 				ramdisk_flags |= RD_PROMPT;
86 		else
87 				ramdisk_flags &= ~RD_PROMPT;
88 
89 		val = sh_check_cmd_arg(bootargs, CMD_ARG_RD_DOLOAD, 10);
90 		if (val == 1)
91 				ramdisk_flags |= RD_DOLOAD;
92 		else
93 				ramdisk_flags &= ~RD_DOLOAD;
94 
95 		set_sh_linux_param((unsigned long)param + MOUNT_ROOT_RDONLY, 0x0001);
96 		set_sh_linux_param((unsigned long)param + RAMDISK_FLAGS, ramdisk_flags);
97 		set_sh_linux_param((unsigned long)param + ORIG_ROOT_DEV, 0x0200);
98 		set_sh_linux_param((unsigned long)param + LOADER_TYPE, 0x0001);
99 		set_sh_linux_param((unsigned long)param + INITRD_START,
100 			GET_INITRD_START(images->rd_start, CONFIG_SYS_SDRAM_BASE));
101 		set_sh_linux_param((unsigned long)param + INITRD_SIZE,
102 			images->rd_end - images->rd_start);
103 	}
104 
105 	/* Boot kernel */
106 	kernel();
107 
108 	/* does not return */
109 	return 1;
110 }
111