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