blob: f01d4df8bc664e4150dba6cf6a0b1615b68019a9 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040014#include <mapmem.h>
Ilya Yanokf7a2c552012-09-18 00:22:50 +000015#include <spl.h>
16#include <net.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Ilya Yanokf7a2c552012-09-18 00:22:50 +000018
Simon Glasse5cd9a42021-07-10 21:14:26 -060019#if defined(CONFIG_SPL_ETH) || defined(CONFIG_SPL_USB_ETHER)
Andrew F. Davis0a88c802017-04-07 14:29:36 -050020static ulong spl_net_load_read(struct spl_load_info *load, ulong sector,
21 ulong count, void *buf)
22{
23 debug("%s: sector %lx, count %lx, buf %lx\n",
24 __func__, sector, count, (ulong)buf);
Sean Anderson5ff77722023-10-14 16:47:55 -040025 memcpy(buf, map_sysmem(image_load_addr + sector, count), count);
Andrew F. Davis0a88c802017-04-07 14:29:36 -050026 return count;
27}
28
Simon Glassee306792016-09-24 18:20:13 -060029static int spl_net_load_image(struct spl_image_info *spl_image,
30 struct spl_boot_device *bootdev)
Ilya Yanokf7a2c552012-09-18 00:22:50 +000031{
Sean Anderson5ff77722023-10-14 16:47:55 -040032 struct legacy_img_hdr *header = map_sysmem(image_load_addr,
33 sizeof(*header));
Ilya Yanokf7a2c552012-09-18 00:22:50 +000034 int rv;
35
36 env_init();
37 env_relocate();
Simon Glass6a38e412017-08-03 12:22:09 -060038 env_set("autoload", "yes");
Joe Hershberger3dbe17e2015-03-22 17:09:06 -050039 rv = eth_initialize();
Ilya Yanokf7a2c552012-09-18 00:22:50 +000040 if (rv == 0) {
41 printf("No Ethernet devices found\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020042 return -ENODEV;
Ilya Yanokf7a2c552012-09-18 00:22:50 +000043 }
Simon Glassc0b6c9b2016-09-24 18:19:57 -060044 if (bootdev->boot_device_name)
Simon Glass6a38e412017-08-03 12:22:09 -060045 env_set("ethact", bootdev->boot_device_name);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050046 rv = net_loop(BOOTP);
Ilya Yanokf7a2c552012-09-18 00:22:50 +000047 if (rv < 0) {
48 printf("Problem booting with BOOTP\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020049 return rv;
Ilya Yanokf7a2c552012-09-18 00:22:50 +000050 }
Andrew F. Davis0a88c802017-04-07 14:29:36 -050051
52 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
53 image_get_magic(header) == FDT_MAGIC) {
54 struct spl_load_info load;
55
56 debug("Found FIT\n");
57 load.bl_len = 1;
58 load.read = spl_net_load_read;
59 rv = spl_load_simple_fit(spl_image, &load, 0, header);
60 } else {
61 debug("Legacy image\n");
62
Pali Rohárdda8f882022-01-14 14:31:38 +010063 rv = spl_parse_image_header(spl_image, bootdev, header);
Andrew F. Davis0a88c802017-04-07 14:29:36 -050064 if (rv)
65 return rv;
66
Sean Anderson5ff77722023-10-14 16:47:55 -040067 memcpy(map_sysmem(spl_image->load_addr, spl_image->size),
68 map_sysmem(image_load_addr, spl_image->size),
69 spl_image->size);
Andrew F. Davis0a88c802017-04-07 14:29:36 -050070 }
71
72 return rv;
Ilya Yanokf7a2c552012-09-18 00:22:50 +000073}
Simon Glassd9aefcb2016-09-24 18:20:11 -060074#endif
75
Simon Glasse5cd9a42021-07-10 21:14:26 -060076#ifdef CONFIG_SPL_ETH
Simon Glassee306792016-09-24 18:20:13 -060077int spl_net_load_image_cpgmac(struct spl_image_info *spl_image,
78 struct spl_boot_device *bootdev)
Simon Glassd9aefcb2016-09-24 18:20:11 -060079{
80#ifdef CONFIG_SPL_ETH_DEVICE
81 bootdev->boot_device_name = CONFIG_SPL_ETH_DEVICE;
82#endif
83
Simon Glassee306792016-09-24 18:20:13 -060084 return spl_net_load_image(spl_image, bootdev);
Simon Glassd9aefcb2016-09-24 18:20:11 -060085}
Simon Glass4fc1f252016-11-30 15:30:50 -070086SPL_LOAD_IMAGE_METHOD("eth device", 0, BOOT_DEVICE_CPGMAC,
87 spl_net_load_image_cpgmac);
Simon Glassd9aefcb2016-09-24 18:20:11 -060088#endif
89
Faiz Abbasc01553b2018-02-16 21:17:44 +053090#ifdef CONFIG_SPL_USB_ETHER
Simon Glassee306792016-09-24 18:20:13 -060091int spl_net_load_image_usb(struct spl_image_info *spl_image,
92 struct spl_boot_device *bootdev)
Simon Glassd9aefcb2016-09-24 18:20:11 -060093{
94 bootdev->boot_device_name = "usb_ether";
Jean-Jacques Hiblot2c753142018-12-04 11:30:48 +010095#if CONFIG_IS_ENABLED(DM_USB_GADGET)
96 usb_ether_init();
97#endif
Simon Glassee306792016-09-24 18:20:13 -060098 return spl_net_load_image(spl_image, bootdev);
Simon Glassd9aefcb2016-09-24 18:20:11 -060099}
Simon Glass4fc1f252016-11-30 15:30:50 -0700100SPL_LOAD_IMAGE_METHOD("USB eth", 0, BOOT_DEVICE_USBETH, spl_net_load_image_usb);
Simon Glassd9aefcb2016-09-24 18:20:11 -0600101#endif