1// SPDX-License-Identifier: GPL-2.0+
2/*
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>
8 */
9#include <common.h>
10#include <env.h>
11#include <errno.h>
12#include <image.h>
13#include <log.h>
14#include <spl.h>
15#include <spl_load.h>
16#include <net.h>
17#include <linux/libfdt.h>
18
19#if defined(CONFIG_SPL_ETH) || defined(CONFIG_SPL_USB_ETHER)
20static 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);
25	memcpy(buf, map_sysmem(image_load_addr + sector, count), count);
26	return count;
27}
28
29static int spl_net_load_image(struct spl_image_info *spl_image,
30			      struct spl_boot_device *bootdev)
31{
32	struct spl_load_info load;
33	int rv;
34
35	env_init();
36	env_relocate();
37	env_set("autoload", "yes");
38	rv = eth_initialize();
39	if (rv == 0) {
40		printf("No Ethernet devices found\n");
41		return -ENODEV;
42	}
43	if (bootdev->boot_device_name)
44		env_set("ethact", bootdev->boot_device_name);
45	rv = net_loop(BOOTP);
46	if (rv < 0) {
47		printf("Problem booting with BOOTP\n");
48		return rv;
49	}
50
51	spl_set_bl_len(&load, 1);
52	load.read = spl_net_load_read;
53	return spl_load(spl_image, bootdev, &load, 0, 0);
54}
55#endif
56
57#ifdef CONFIG_SPL_ETH
58int spl_net_load_image_cpgmac(struct spl_image_info *spl_image,
59			      struct spl_boot_device *bootdev)
60{
61#ifdef CONFIG_SPL_ETH_DEVICE
62	bootdev->boot_device_name = CONFIG_SPL_ETH_DEVICE;
63#endif
64
65	return spl_net_load_image(spl_image, bootdev);
66}
67SPL_LOAD_IMAGE_METHOD("eth device", 0, BOOT_DEVICE_CPGMAC,
68		      spl_net_load_image_cpgmac);
69#endif
70
71#ifdef CONFIG_SPL_USB_ETHER
72int spl_net_load_image_usb(struct spl_image_info *spl_image,
73			   struct spl_boot_device *bootdev)
74{
75	bootdev->boot_device_name = "usb_ether";
76#if CONFIG_IS_ENABLED(DM_USB_GADGET)
77	usb_ether_init();
78#endif
79	return spl_net_load_image(spl_image, bootdev);
80}
81SPL_LOAD_IMAGE_METHOD("USB eth", 0, BOOT_DEVICE_USBETH, spl_net_load_image_usb);
82#endif
83