blob: a9b6b07ab300f60619ea2b1b65de0207df5b3a77 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ilya Yanokf7a2c552012-09-18 00:22:50 +00002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2012
7 * Ilya Yanok <ilya.yanok@gmail.com>
Ilya Yanokf7a2c552012-09-18 00:22:50 +00008 */
9#include <common.h>
Simon Glass79fd2142019-08-01 09:46:43 -060010#include <env.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020011#include <errno.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060012#include <image.h>
Ilya Yanokf7a2c552012-09-18 00:22:50 +000013#include <spl.h>
14#include <net.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt.h>
Ilya Yanokf7a2c552012-09-18 00:22:50 +000016
Faiz Abbasc01553b2018-02-16 21:17:44 +053017#if defined(CONFIG_SPL_ETH_SUPPORT) || defined(CONFIG_SPL_USB_ETHER)
Andrew F. Davis0a88c802017-04-07 14:29:36 -050018static ulong spl_net_load_read(struct spl_load_info *load, ulong sector,
19 ulong count, void *buf)
20{
21 debug("%s: sector %lx, count %lx, buf %lx\n",
22 __func__, sector, count, (ulong)buf);
Simon Glass892265d2019-12-28 10:45:02 -070023 memcpy(buf, (void *)(image_load_addr + sector), count);
Andrew F. Davis0a88c802017-04-07 14:29:36 -050024 return count;
25}
26
Simon Glassee306792016-09-24 18:20:13 -060027static int spl_net_load_image(struct spl_image_info *spl_image,
28 struct spl_boot_device *bootdev)
Ilya Yanokf7a2c552012-09-18 00:22:50 +000029{
Simon Glass892265d2019-12-28 10:45:02 -070030 struct image_header *header = (struct image_header *)image_load_addr;
Ilya Yanokf7a2c552012-09-18 00:22:50 +000031 int rv;
32
33 env_init();
34 env_relocate();
Simon Glass6a38e412017-08-03 12:22:09 -060035 env_set("autoload", "yes");
Joe Hershberger3dbe17e2015-03-22 17:09:06 -050036 rv = eth_initialize();
Ilya Yanokf7a2c552012-09-18 00:22:50 +000037 if (rv == 0) {
38 printf("No Ethernet devices found\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020039 return -ENODEV;
Ilya Yanokf7a2c552012-09-18 00:22:50 +000040 }
Simon Glassc0b6c9b2016-09-24 18:19:57 -060041 if (bootdev->boot_device_name)
Simon Glass6a38e412017-08-03 12:22:09 -060042 env_set("ethact", bootdev->boot_device_name);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050043 rv = net_loop(BOOTP);
Ilya Yanokf7a2c552012-09-18 00:22:50 +000044 if (rv < 0) {
45 printf("Problem booting with BOOTP\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020046 return rv;
Ilya Yanokf7a2c552012-09-18 00:22:50 +000047 }
Andrew F. Davis0a88c802017-04-07 14:29:36 -050048
49 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
50 image_get_magic(header) == FDT_MAGIC) {
51 struct spl_load_info load;
52
53 debug("Found FIT\n");
54 load.bl_len = 1;
55 load.read = spl_net_load_read;
56 rv = spl_load_simple_fit(spl_image, &load, 0, header);
57 } else {
58 debug("Legacy image\n");
59
60 rv = spl_parse_image_header(spl_image, header);
61 if (rv)
62 return rv;
63
64 memcpy((void *)spl_image->load_addr, header, spl_image->size);
65 }
66
67 return rv;
Ilya Yanokf7a2c552012-09-18 00:22:50 +000068}
Simon Glassd9aefcb2016-09-24 18:20:11 -060069#endif
70
71#ifdef CONFIG_SPL_ETH_SUPPORT
Simon Glassee306792016-09-24 18:20:13 -060072int spl_net_load_image_cpgmac(struct spl_image_info *spl_image,
73 struct spl_boot_device *bootdev)
Simon Glassd9aefcb2016-09-24 18:20:11 -060074{
75#ifdef CONFIG_SPL_ETH_DEVICE
76 bootdev->boot_device_name = CONFIG_SPL_ETH_DEVICE;
77#endif
78
Simon Glassee306792016-09-24 18:20:13 -060079 return spl_net_load_image(spl_image, bootdev);
Simon Glassd9aefcb2016-09-24 18:20:11 -060080}
Simon Glass4fc1f252016-11-30 15:30:50 -070081SPL_LOAD_IMAGE_METHOD("eth device", 0, BOOT_DEVICE_CPGMAC,
82 spl_net_load_image_cpgmac);
Simon Glassd9aefcb2016-09-24 18:20:11 -060083#endif
84
Faiz Abbasc01553b2018-02-16 21:17:44 +053085#ifdef CONFIG_SPL_USB_ETHER
Simon Glassee306792016-09-24 18:20:13 -060086int spl_net_load_image_usb(struct spl_image_info *spl_image,
87 struct spl_boot_device *bootdev)
Simon Glassd9aefcb2016-09-24 18:20:11 -060088{
89 bootdev->boot_device_name = "usb_ether";
Jean-Jacques Hiblot2c753142018-12-04 11:30:48 +010090#if CONFIG_IS_ENABLED(DM_USB_GADGET)
91 usb_ether_init();
92#endif
Simon Glassee306792016-09-24 18:20:13 -060093 return spl_net_load_image(spl_image, bootdev);
Simon Glassd9aefcb2016-09-24 18:20:11 -060094}
Simon Glass4fc1f252016-11-30 15:30:50 -070095SPL_LOAD_IMAGE_METHOD("USB eth", 0, BOOT_DEVICE_USBETH, spl_net_load_image_usb);
Simon Glassd9aefcb2016-09-24 18:20:11 -060096#endif