1 /*
2  * (C) Copyright 2014
3  * NVIDIA Corporation <www.nvidia.com>
4  *
5  * Copyright 2014 Red Hat, Inc.
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9 
10 #ifndef _CONFIG_CMD_DISTRO_BOOTCMD_H
11 #define _CONFIG_CMD_DISTRO_BOOTCMD_H
12 
13 /*
14  * A note on error handling: It is possible for BOOT_TARGET_DEVICES to
15  * reference a device that is not enabled in the U-Boot configuration, e.g.
16  * it may include MMC in the list without CONFIG_CMD_MMC being enabled. Given
17  * that BOOT_TARGET_DEVICES is a macro that's expanded by the C pre-processor
18  * at compile time, it's not  possible to detect and report such problems via
19  * a simple #ifdef/#error combination. Still, the code needs to report errors.
20  * The best way I've found to do this is to make BOOT_TARGET_DEVICES expand to
21  * reference a non-existent symbol, and have the name of that symbol encode
22  * the error message. Consequently, this file contains references to e.g.
23  * BOOT_TARGET_DEVICES_references_MMC_without_CONFIG_CMD_MMC. Given the
24  * prevalence of capitals here, this looks like a pre-processor macro and
25  * hence seems like it should be all capitals, but it's really an error
26  * message that includes some other pre-processor symbols in the text.
27  */
28 
29 #define BOOTENV_SHARED_BLKDEV_BODY(devtypel) \
30 		"if " #devtypel " dev ${devnum}; then " \
31 			"setenv devtype " #devtypel "; " \
32 			"run scan_dev_for_boot_part; " \
33 		"fi\0"
34 
35 #define BOOTENV_SHARED_BLKDEV(devtypel) \
36 	#devtypel "_boot=" \
37 	BOOTENV_SHARED_BLKDEV_BODY(devtypel)
38 
39 #define BOOTENV_DEV_BLKDEV(devtypeu, devtypel, instance) \
40 	"bootcmd_" #devtypel #instance "=" \
41 		"setenv devnum " #instance "; " \
42 		"run " #devtypel "_boot\0"
43 
44 #define BOOTENV_DEV_NAME_BLKDEV(devtypeu, devtypel, instance) \
45 	#devtypel #instance " "
46 
47 #ifdef CONFIG_SANDBOX
48 #define BOOTENV_SHARED_HOST	BOOTENV_SHARED_BLKDEV(host)
49 #define BOOTENV_DEV_HOST	BOOTENV_DEV_BLKDEV
50 #define BOOTENV_DEV_NAME_HOST	BOOTENV_DEV_NAME_BLKDEV
51 #else
52 #define BOOTENV_SHARED_HOST
53 #define BOOTENV_DEV_HOST \
54 	BOOT_TARGET_DEVICES_references_HOST_without_CONFIG_SANDBOX
55 #define BOOTENV_DEV_NAME_HOST \
56 	BOOT_TARGET_DEVICES_references_HOST_without_CONFIG_SANDBOX
57 #endif
58 
59 #ifdef CONFIG_CMD_MMC
60 #define BOOTENV_SHARED_MMC	BOOTENV_SHARED_BLKDEV(mmc)
61 #define BOOTENV_DEV_MMC		BOOTENV_DEV_BLKDEV
62 #define BOOTENV_DEV_NAME_MMC	BOOTENV_DEV_NAME_BLKDEV
63 #else
64 #define BOOTENV_SHARED_MMC
65 #define BOOTENV_DEV_MMC \
66 	BOOT_TARGET_DEVICES_references_MMC_without_CONFIG_CMD_MMC
67 #define BOOTENV_DEV_NAME_MMC \
68 	BOOT_TARGET_DEVICES_references_MMC_without_CONFIG_CMD_MMC
69 #endif
70 
71 #ifdef CONFIG_CMD_UBIFS
72 #define BOOTENV_SHARED_UBIFS \
73 	"ubifs_boot=" \
74 		"env exists bootubipart || " \
75 			"env set bootubipart UBI; " \
76 		"env exists bootubivol || " \
77 			"env set bootubivol boot; " \
78 		"if ubi part ${bootubipart} && " \
79 			"ubifsmount ubi${devnum}:${bootubivol}; " \
80 		"then " \
81 			"setenv devtype ubi; " \
82 			"run scan_dev_for_boot; " \
83 		"fi\0"
84 #define BOOTENV_DEV_UBIFS	BOOTENV_DEV_BLKDEV
85 #define BOOTENV_DEV_NAME_UBIFS	BOOTENV_DEV_NAME_BLKDEV
86 #else
87 #define BOOTENV_SHARED_UBIFS
88 #define BOOTENV_DEV_UBIFS \
89 	BOOT_TARGET_DEVICES_references_UBIFS_without_CONFIG_CMD_UBIFS
90 #define BOOTENV_DEV_NAME_UBIFS \
91 	BOOT_TARGET_DEVICES_references_UBIFS_without_CONFIG_CMD_UBIFS
92 #endif
93 
94 #ifdef CONFIG_EFI_LOADER
95 #if defined(CONFIG_ARM64)
96 #define BOOTEFI_NAME "bootaa64.efi"
97 #elif defined(CONFIG_ARM)
98 #define BOOTEFI_NAME "bootarm.efi"
99 #elif defined(CONFIG_X86_RUN_32BIT)
100 #define BOOTEFI_NAME "bootia32.efi"
101 #elif defined(CONFIG_X86_RUN_64BIT)
102 #define BOOTEFI_NAME "bootx64.efi"
103 #endif
104 #endif
105 
106 #ifdef BOOTEFI_NAME
107 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
108 /*
109  * On 32bit ARM systems there is a reasonable number of systems that follow
110  * the $soc-$board$boardver.dtb name scheme for their device trees. Use that
111  * scheme if we don't have an explicit fdtfile variable.
112  */
113 #define BOOTENV_EFI_SET_FDTFILE_FALLBACK                                  \
114 	"if test -z \"${fdtfile}\" -a -n \"${soc}\"; then "               \
115 	  "setenv efi_fdtfile ${soc}-${board}${boardver}.dtb; "           \
116 	"fi; "
117 #else
118 #define BOOTENV_EFI_SET_FDTFILE_FALLBACK
119 #endif
120 
121 
122 #define BOOTENV_SHARED_EFI                                                \
123 	"boot_efi_binary="                                                \
124 		"if fdt addr ${fdt_addr_r}; then "                        \
125 			"bootefi bootmgr ${fdt_addr_r};"                  \
126 		"else "                                                   \
127 			"bootefi bootmgr ${fdtcontroladdr};"              \
128 		"fi;"                                                     \
129 		"load ${devtype} ${devnum}:${distro_bootpart} "           \
130 			"${kernel_addr_r} efi/boot/"BOOTEFI_NAME"; "      \
131 		"if fdt addr ${fdt_addr_r}; then "                        \
132 			"bootefi ${kernel_addr_r} ${fdt_addr_r};"         \
133 		"else "                                                   \
134 			"bootefi ${kernel_addr_r} ${fdtcontroladdr};"     \
135 		"fi\0"                                                    \
136 	\
137 	"load_efi_dtb="                                                   \
138 		"load ${devtype} ${devnum}:${distro_bootpart} "           \
139 			"${fdt_addr_r} ${prefix}${efi_fdtfile}\0"         \
140 	\
141 	"efi_dtb_prefixes=/ /dtb/ /dtb/current/\0"                        \
142 	"scan_dev_for_efi="                                               \
143 		"setenv efi_fdtfile ${fdtfile}; "                         \
144 		BOOTENV_EFI_SET_FDTFILE_FALLBACK                          \
145 		"for prefix in ${efi_dtb_prefixes}; do "                  \
146 			"if test -e ${devtype} "                          \
147 					"${devnum}:${distro_bootpart} "   \
148 					"${prefix}${efi_fdtfile}; then "  \
149 				"run load_efi_dtb; "                      \
150 			"fi;"                                             \
151 		"done;"                                                   \
152 		"if test -e ${devtype} ${devnum}:${distro_bootpart} "     \
153 					"efi/boot/"BOOTEFI_NAME"; then "  \
154 				"echo Found EFI removable media binary "  \
155 					"efi/boot/"BOOTEFI_NAME"; "       \
156 				"run boot_efi_binary; "                   \
157 				"echo EFI LOAD FAILED: continuing...; "   \
158 		"fi; "                                                    \
159 		"setenv efi_fdtfile\0"
160 #define SCAN_DEV_FOR_EFI "run scan_dev_for_efi;"
161 #else
162 #define BOOTENV_SHARED_EFI
163 #define SCAN_DEV_FOR_EFI
164 #endif
165 
166 #ifdef CONFIG_SATA
167 #define BOOTENV_SHARED_SATA	BOOTENV_SHARED_BLKDEV(sata)
168 #define BOOTENV_DEV_SATA	BOOTENV_DEV_BLKDEV
169 #define BOOTENV_DEV_NAME_SATA	BOOTENV_DEV_NAME_BLKDEV
170 #else
171 #define BOOTENV_SHARED_SATA
172 #define BOOTENV_DEV_SATA \
173 	BOOT_TARGET_DEVICES_references_SATA_without_CONFIG_SATA
174 #define BOOTENV_DEV_NAME_SATA \
175 	BOOT_TARGET_DEVICES_references_SATA_without_CONFIG_SATA
176 #endif
177 
178 #ifdef CONFIG_SCSI
179 #define BOOTENV_RUN_SCSI_INIT "run scsi_init; "
180 #define BOOTENV_SET_SCSI_NEED_INIT "setenv scsi_need_init; "
181 #define BOOTENV_SHARED_SCSI \
182 	"scsi_init=" \
183 		"if ${scsi_need_init}; then " \
184 			"setenv scsi_need_init false; " \
185 			"scsi scan; " \
186 		"fi\0" \
187 	\
188 	"scsi_boot=" \
189 		BOOTENV_RUN_SCSI_INIT \
190 		BOOTENV_SHARED_BLKDEV_BODY(scsi)
191 #define BOOTENV_DEV_SCSI	BOOTENV_DEV_BLKDEV
192 #define BOOTENV_DEV_NAME_SCSI	BOOTENV_DEV_NAME_BLKDEV
193 #else
194 #define BOOTENV_RUN_SCSI_INIT
195 #define BOOTENV_SET_SCSI_NEED_INIT
196 #define BOOTENV_SHARED_SCSI
197 #define BOOTENV_DEV_SCSI \
198 	BOOT_TARGET_DEVICES_references_SCSI_without_CONFIG_SCSI
199 #define BOOTENV_DEV_NAME_SCSI \
200 	BOOT_TARGET_DEVICES_references_SCSI_without_CONFIG_SCSI
201 #endif
202 
203 #ifdef CONFIG_IDE
204 #define BOOTENV_SHARED_IDE	BOOTENV_SHARED_BLKDEV(ide)
205 #define BOOTENV_DEV_IDE		BOOTENV_DEV_BLKDEV
206 #define BOOTENV_DEV_NAME_IDE	BOOTENV_DEV_NAME_BLKDEV
207 #else
208 #define BOOTENV_SHARED_IDE
209 #define BOOTENV_DEV_IDE \
210 	BOOT_TARGET_DEVICES_references_IDE_without_CONFIG_IDE
211 #define BOOTENV_DEV_NAME_IDE \
212 	BOOT_TARGET_DEVICES_references_IDE_without_CONFIG_IDE
213 #endif
214 
215 #if defined(CONFIG_DM_PCI)
216 #define BOOTENV_RUN_NET_PCI_ENUM "run boot_net_pci_enum; "
217 #define BOOTENV_SHARED_PCI \
218 	"boot_net_pci_enum=pci enum\0"
219 #else
220 #define BOOTENV_RUN_NET_PCI_ENUM
221 #define BOOTENV_SHARED_PCI
222 #endif
223 
224 #ifdef CONFIG_CMD_USB
225 #define BOOTENV_RUN_NET_USB_START "run boot_net_usb_start; "
226 #define BOOTENV_SHARED_USB \
227 	"boot_net_usb_start=usb start\0" \
228 	"usb_boot=" \
229 		"usb start; " \
230 		BOOTENV_SHARED_BLKDEV_BODY(usb)
231 #define BOOTENV_DEV_USB		BOOTENV_DEV_BLKDEV
232 #define BOOTENV_DEV_NAME_USB	BOOTENV_DEV_NAME_BLKDEV
233 #else
234 #define BOOTENV_RUN_NET_USB_START
235 #define BOOTENV_SHARED_USB
236 #define BOOTENV_DEV_USB \
237 	BOOT_TARGET_DEVICES_references_USB_without_CONFIG_CMD_USB
238 #define BOOTENV_DEV_NAME_USB \
239 	BOOT_TARGET_DEVICES_references_USB_without_CONFIG_CMD_USB
240 #endif
241 
242 #if defined(CONFIG_CMD_DHCP)
243 #if defined(CONFIG_EFI_LOADER)
244 #if defined(CONFIG_ARM64)
245 #define BOOTENV_EFI_PXE_ARCH "0xb"
246 #define BOOTENV_EFI_PXE_VCI "PXEClient:Arch:00011:UNDI:003000"
247 #elif defined(CONFIG_ARM)
248 #define BOOTENV_EFI_PXE_ARCH "0xa"
249 #define BOOTENV_EFI_PXE_VCI "PXEClient:Arch:00010:UNDI:003000"
250 #elif defined(CONFIG_X86)
251 /* Always assume we're running 64bit */
252 #define BOOTENV_EFI_PXE_ARCH "0x7"
253 #define BOOTENV_EFI_PXE_VCI "PXEClient:Arch:00007:UNDI:003000"
254 #else
255 #error Please specify an EFI client identifier
256 #endif
257 
258 /*
259  * Ask the dhcp server for an EFI binary. If we get one, check for a
260  * device tree in the same folder. Then boot everything. If the file was
261  * not an EFI binary, we just return from the bootefi command and continue.
262  */
263 #define BOOTENV_EFI_RUN_DHCP \
264 	"setenv efi_fdtfile ${fdtfile}; "                                 \
265 	BOOTENV_EFI_SET_FDTFILE_FALLBACK                                  \
266 	"setenv efi_old_vci ${bootp_vci};"                                \
267 	"setenv efi_old_arch ${bootp_arch};"                              \
268 	"setenv bootp_vci " BOOTENV_EFI_PXE_VCI ";"                       \
269 	"setenv bootp_arch " BOOTENV_EFI_PXE_ARCH ";"                     \
270 	"if dhcp ${kernel_addr_r}; then "                                 \
271 		"tftpboot ${fdt_addr_r} dtb/${efi_fdtfile};"              \
272 		"if fdt addr ${fdt_addr_r}; then "                        \
273 			"bootefi ${kernel_addr_r} ${fdt_addr_r}; "        \
274 		"else "                                                   \
275 			"bootefi ${kernel_addr_r} ${fdtcontroladdr};"     \
276 		"fi;"                                                     \
277 	"fi;"                                                             \
278 	"setenv bootp_vci ${efi_old_vci};"                                \
279 	"setenv bootp_arch ${efi_old_arch};"                              \
280 	"setenv efi_fdtfile;"                                             \
281 	"setenv efi_old_arch;"                                            \
282 	"setenv efi_old_vci;"
283 #else
284 #define BOOTENV_EFI_RUN_DHCP
285 #endif
286 #define BOOTENV_DEV_DHCP(devtypeu, devtypel, instance) \
287 	"bootcmd_dhcp=" \
288 		BOOTENV_RUN_NET_USB_START \
289 		BOOTENV_RUN_NET_PCI_ENUM \
290 		"if dhcp ${scriptaddr} ${boot_script_dhcp}; then " \
291 			"source ${scriptaddr}; " \
292 		"fi;" \
293 		BOOTENV_EFI_RUN_DHCP \
294 		"\0"
295 #define BOOTENV_DEV_NAME_DHCP(devtypeu, devtypel, instance) \
296 	"dhcp "
297 #else
298 #define BOOTENV_DEV_DHCP \
299 	BOOT_TARGET_DEVICES_references_DHCP_without_CONFIG_CMD_DHCP
300 #define BOOTENV_DEV_NAME_DHCP \
301 	BOOT_TARGET_DEVICES_references_DHCP_without_CONFIG_CMD_DHCP
302 #endif
303 
304 #if defined(CONFIG_CMD_DHCP) && defined(CONFIG_CMD_PXE)
305 #define BOOTENV_DEV_PXE(devtypeu, devtypel, instance) \
306 	"bootcmd_pxe=" \
307 		BOOTENV_RUN_NET_USB_START \
308 		BOOTENV_RUN_NET_PCI_ENUM \
309 		"dhcp; " \
310 		"if pxe get; then " \
311 			"pxe boot; " \
312 		"fi\0"
313 #define BOOTENV_DEV_NAME_PXE(devtypeu, devtypel, instance) \
314 	"pxe "
315 #else
316 #define BOOTENV_DEV_PXE \
317 	BOOT_TARGET_DEVICES_references_PXE_without_CONFIG_CMD_DHCP_or_PXE
318 #define BOOTENV_DEV_NAME_PXE \
319 	BOOT_TARGET_DEVICES_references_PXE_without_CONFIG_CMD_DHCP_or_PXE
320 #endif
321 
322 #define BOOTENV_DEV_NAME(devtypeu, devtypel, instance) \
323 	BOOTENV_DEV_NAME_##devtypeu(devtypeu, devtypel, instance)
324 #define BOOTENV_BOOT_TARGETS \
325 	"boot_targets=" BOOT_TARGET_DEVICES(BOOTENV_DEV_NAME) "\0"
326 
327 #define BOOTENV_DEV(devtypeu, devtypel, instance) \
328 	BOOTENV_DEV_##devtypeu(devtypeu, devtypel, instance)
329 #define BOOTENV \
330 	BOOTENV_SHARED_HOST \
331 	BOOTENV_SHARED_MMC \
332 	BOOTENV_SHARED_PCI \
333 	BOOTENV_SHARED_USB \
334 	BOOTENV_SHARED_SATA \
335 	BOOTENV_SHARED_SCSI \
336 	BOOTENV_SHARED_IDE \
337 	BOOTENV_SHARED_UBIFS \
338 	BOOTENV_SHARED_EFI \
339 	"boot_prefixes=/ /boot/\0" \
340 	"boot_scripts=boot.scr.uimg boot.scr\0" \
341 	"boot_script_dhcp=boot.scr.uimg\0" \
342 	BOOTENV_BOOT_TARGETS \
343 	\
344 	"boot_extlinux="                                                  \
345 		"sysboot ${devtype} ${devnum}:${distro_bootpart} any "    \
346 			"${scriptaddr} ${prefix}extlinux/extlinux.conf\0" \
347 	\
348 	"scan_dev_for_extlinux="                                          \
349 		"if test -e ${devtype} "                                  \
350 				"${devnum}:${distro_bootpart} "           \
351 				"${prefix}extlinux/extlinux.conf; then "  \
352 			"echo Found ${prefix}extlinux/extlinux.conf; "    \
353 			"run boot_extlinux; "                             \
354 			"echo SCRIPT FAILED: continuing...; "             \
355 		"fi\0"                                                    \
356 	\
357 	"boot_a_script="                                                  \
358 		"load ${devtype} ${devnum}:${distro_bootpart} "           \
359 			"${scriptaddr} ${prefix}${script}; "              \
360 		"source ${scriptaddr}\0"                                  \
361 	\
362 	"scan_dev_for_scripts="                                           \
363 		"for script in ${boot_scripts}; do "                      \
364 			"if test -e ${devtype} "                          \
365 					"${devnum}:${distro_bootpart} "   \
366 					"${prefix}${script}; then "       \
367 				"echo Found U-Boot script "               \
368 					"${prefix}${script}; "            \
369 				"run boot_a_script; "                     \
370 				"echo SCRIPT FAILED: continuing...; "     \
371 			"fi; "                                            \
372 		"done\0"                                                  \
373 	\
374 	"scan_dev_for_boot="                                              \
375 		"echo Scanning ${devtype} "                               \
376 				"${devnum}:${distro_bootpart}...; "       \
377 		"for prefix in ${boot_prefixes}; do "                     \
378 			"run scan_dev_for_extlinux; "                     \
379 			"run scan_dev_for_scripts; "                      \
380 		"done;"                                                   \
381 		SCAN_DEV_FOR_EFI                                          \
382 		"\0"                                                      \
383 	\
384 	"scan_dev_for_boot_part="                                         \
385 		"part list ${devtype} ${devnum} -bootable devplist; "     \
386 		"env exists devplist || setenv devplist 1; "              \
387 		"for distro_bootpart in ${devplist}; do "                 \
388 			"if fstype ${devtype} "                           \
389 					"${devnum}:${distro_bootpart} "   \
390 					"bootfstype; then "               \
391 				"run scan_dev_for_boot; "                 \
392 			"fi; "                                            \
393 		"done\0"                                                  \
394 	\
395 	BOOT_TARGET_DEVICES(BOOTENV_DEV)                                  \
396 	\
397 	"distro_bootcmd=" BOOTENV_SET_SCSI_NEED_INIT                      \
398 		"for target in ${boot_targets}; do "                      \
399 			"run bootcmd_${target}; "                         \
400 		"done\0"
401 
402 #ifndef CONFIG_BOOTCOMMAND
403 #define CONFIG_BOOTCOMMAND "run distro_bootcmd"
404 #endif
405 
406 #endif  /* _CONFIG_CMD_DISTRO_BOOTCMD_H */
407