blob: e140a6306ffff8c8f7192855e7347656b97b8b7d [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>
Simon Glass4d72caa2020-05-10 11:40:01 -060012#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000014#include <spl.h>
15#include <net.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000017
Faiz Abbasb432b1e2018-02-16 21:17:44 +053018#if defined(CONFIG_SPL_ETH_SUPPORT) || defined(CONFIG_SPL_USB_ETHER)
Andrew F. Davis6dca5e82017-04-07 14:29:36 -050019static ulong spl_net_load_read(struct spl_load_info *load, ulong sector,
20 ulong count, void *buf)
21{
22 debug("%s: sector %lx, count %lx, buf %lx\n",
23 __func__, sector, count, (ulong)buf);
Simon Glassbb872dd2019-12-28 10:45:02 -070024 memcpy(buf, (void *)(image_load_addr + sector), count);
Andrew F. Davis6dca5e82017-04-07 14:29:36 -050025 return count;
26}
27
Simon Glass2a2ee2a2016-09-24 18:20:13 -060028static int spl_net_load_image(struct spl_image_info *spl_image,
29 struct spl_boot_device *bootdev)
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000030{
Simon Glassbb872dd2019-12-28 10:45:02 -070031 struct image_header *header = (struct image_header *)image_load_addr;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000032 int rv;
33
34 env_init();
35 env_relocate();
Simon Glass382bee52017-08-03 12:22:09 -060036 env_set("autoload", "yes");
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050037 rv = eth_initialize();
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000038 if (rv == 0) {
39 printf("No Ethernet devices found\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020040 return -ENODEV;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000041 }
Simon Glassecdfd692016-09-24 18:19:57 -060042 if (bootdev->boot_device_name)
Simon Glass382bee52017-08-03 12:22:09 -060043 env_set("ethact", bootdev->boot_device_name);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050044 rv = net_loop(BOOTP);
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000045 if (rv < 0) {
46 printf("Problem booting with BOOTP\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020047 return rv;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000048 }
Andrew F. Davis6dca5e82017-04-07 14:29:36 -050049
50 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
51 image_get_magic(header) == FDT_MAGIC) {
52 struct spl_load_info load;
53
54 debug("Found FIT\n");
55 load.bl_len = 1;
56 load.read = spl_net_load_read;
57 rv = spl_load_simple_fit(spl_image, &load, 0, header);
58 } else {
59 debug("Legacy image\n");
60
61 rv = spl_parse_image_header(spl_image, header);
62 if (rv)
63 return rv;
64
65 memcpy((void *)spl_image->load_addr, header, spl_image->size);
66 }
67
68 return rv;
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000069}
Simon Glass7ec03892016-09-24 18:20:11 -060070#endif
71
72#ifdef CONFIG_SPL_ETH_SUPPORT
Simon Glass2a2ee2a2016-09-24 18:20:13 -060073int spl_net_load_image_cpgmac(struct spl_image_info *spl_image,
74 struct spl_boot_device *bootdev)
Simon Glass7ec03892016-09-24 18:20:11 -060075{
76#ifdef CONFIG_SPL_ETH_DEVICE
77 bootdev->boot_device_name = CONFIG_SPL_ETH_DEVICE;
78#endif
79
Simon Glass2a2ee2a2016-09-24 18:20:13 -060080 return spl_net_load_image(spl_image, bootdev);
Simon Glass7ec03892016-09-24 18:20:11 -060081}
Simon Glassebc4ef62016-11-30 15:30:50 -070082SPL_LOAD_IMAGE_METHOD("eth device", 0, BOOT_DEVICE_CPGMAC,
83 spl_net_load_image_cpgmac);
Simon Glass7ec03892016-09-24 18:20:11 -060084#endif
85
Faiz Abbasb432b1e2018-02-16 21:17:44 +053086#ifdef CONFIG_SPL_USB_ETHER
Simon Glass2a2ee2a2016-09-24 18:20:13 -060087int spl_net_load_image_usb(struct spl_image_info *spl_image,
88 struct spl_boot_device *bootdev)
Simon Glass7ec03892016-09-24 18:20:11 -060089{
90 bootdev->boot_device_name = "usb_ether";
Jean-Jacques Hiblot0f46fb52018-12-04 11:30:48 +010091#if CONFIG_IS_ENABLED(DM_USB_GADGET)
92 usb_ether_init();
93#endif
Simon Glass2a2ee2a2016-09-24 18:20:13 -060094 return spl_net_load_image(spl_image, bootdev);
Simon Glass7ec03892016-09-24 18:20:11 -060095}
Simon Glassebc4ef62016-11-30 15:30:50 -070096SPL_LOAD_IMAGE_METHOD("USB eth", 0, BOOT_DEVICE_USBETH, spl_net_load_image_usb);
Simon Glass7ec03892016-09-24 18:20:11 -060097#endif