blob: 30c050c0b3e6c0767bf1d607ab63037f919da7a7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ilya Yanok7ac2fe22012-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 Yanok7ac2fe22012-09-18 00:22:50 +00008 */
9#include <common.h>
Simon Glass4bfd1f52019-08-01 09:46:43 -060010#include <env.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020011#include <errno.h>
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000012#include <spl.h>
13#include <net.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000015
Faiz Abbasb432b1e2018-02-16 21:17:44 +053016#if defined(CONFIG_SPL_ETH_SUPPORT) || defined(CONFIG_SPL_USB_ETHER)
Andrew F. Davis6dca5e82017-04-07 14:29:36 -050017static ulong spl_net_load_read(struct spl_load_info *load, ulong sector,
18 ulong count, void *buf)
19{
20 debug("%s: sector %lx, count %lx, buf %lx\n",
21 __func__, sector, count, (ulong)buf);
Simon Glassbb872dd2019-12-28 10:45:02 -070022 memcpy(buf, (void *)(image_load_addr + sector), count);
Andrew F. Davis6dca5e82017-04-07 14:29:36 -050023 return count;
24}
25
Simon Glass2a2ee2a2016-09-24 18:20:13 -060026static int spl_net_load_image(struct spl_image_info *spl_image,
27 struct spl_boot_device *bootdev)
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000028{
Simon Glassbb872dd2019-12-28 10:45:02 -070029 struct image_header *header = (struct image_header *)image_load_addr;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000030 int rv;
31
32 env_init();
33 env_relocate();
Simon Glass382bee52017-08-03 12:22:09 -060034 env_set("autoload", "yes");
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050035 rv = eth_initialize();
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000036 if (rv == 0) {
37 printf("No Ethernet devices found\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020038 return -ENODEV;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000039 }
Simon Glassecdfd692016-09-24 18:19:57 -060040 if (bootdev->boot_device_name)
Simon Glass382bee52017-08-03 12:22:09 -060041 env_set("ethact", bootdev->boot_device_name);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050042 rv = net_loop(BOOTP);
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000043 if (rv < 0) {
44 printf("Problem booting with BOOTP\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020045 return rv;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000046 }
Andrew F. Davis6dca5e82017-04-07 14:29:36 -050047
48 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
49 image_get_magic(header) == FDT_MAGIC) {
50 struct spl_load_info load;
51
52 debug("Found FIT\n");
53 load.bl_len = 1;
54 load.read = spl_net_load_read;
55 rv = spl_load_simple_fit(spl_image, &load, 0, header);
56 } else {
57 debug("Legacy image\n");
58
59 rv = spl_parse_image_header(spl_image, header);
60 if (rv)
61 return rv;
62
63 memcpy((void *)spl_image->load_addr, header, spl_image->size);
64 }
65
66 return rv;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000067}
Simon Glass7ec03892016-09-24 18:20:11 -060068#endif
69
70#ifdef CONFIG_SPL_ETH_SUPPORT
Simon Glass2a2ee2a2016-09-24 18:20:13 -060071int spl_net_load_image_cpgmac(struct spl_image_info *spl_image,
72 struct spl_boot_device *bootdev)
Simon Glass7ec03892016-09-24 18:20:11 -060073{
74#ifdef CONFIG_SPL_ETH_DEVICE
75 bootdev->boot_device_name = CONFIG_SPL_ETH_DEVICE;
76#endif
77
Simon Glass2a2ee2a2016-09-24 18:20:13 -060078 return spl_net_load_image(spl_image, bootdev);
Simon Glass7ec03892016-09-24 18:20:11 -060079}
Simon Glassebc4ef62016-11-30 15:30:50 -070080SPL_LOAD_IMAGE_METHOD("eth device", 0, BOOT_DEVICE_CPGMAC,
81 spl_net_load_image_cpgmac);
Simon Glass7ec03892016-09-24 18:20:11 -060082#endif
83
Faiz Abbasb432b1e2018-02-16 21:17:44 +053084#ifdef CONFIG_SPL_USB_ETHER
Simon Glass2a2ee2a2016-09-24 18:20:13 -060085int spl_net_load_image_usb(struct spl_image_info *spl_image,
86 struct spl_boot_device *bootdev)
Simon Glass7ec03892016-09-24 18:20:11 -060087{
88 bootdev->boot_device_name = "usb_ether";
Jean-Jacques Hiblot0f46fb52018-12-04 11:30:48 +010089#if CONFIG_IS_ENABLED(DM_USB_GADGET)
90 usb_ether_init();
91#endif
Simon Glass2a2ee2a2016-09-24 18:20:13 -060092 return spl_net_load_image(spl_image, bootdev);
Simon Glass7ec03892016-09-24 18:20:11 -060093}
Simon Glassebc4ef62016-11-30 15:30:50 -070094SPL_LOAD_IMAGE_METHOD("USB eth", 0, BOOT_DEVICE_USBETH, spl_net_load_image_usb);
Simon Glass7ec03892016-09-24 18:20:11 -060095#endif