common: Drop image.h from common header
[platform/kernel/u-boot.git] / common / spl / spl_net.c
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 <spl.h>
14 #include <net.h>
15 #include <linux/libfdt.h>
16
17 #if defined(CONFIG_SPL_ETH_SUPPORT) || defined(CONFIG_SPL_USB_ETHER)
18 static 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);
23         memcpy(buf, (void *)(image_load_addr + sector), count);
24         return count;
25 }
26
27 static int spl_net_load_image(struct spl_image_info *spl_image,
28                               struct spl_boot_device *bootdev)
29 {
30         struct image_header *header = (struct image_header *)image_load_addr;
31         int rv;
32
33         env_init();
34         env_relocate();
35         env_set("autoload", "yes");
36         rv = eth_initialize();
37         if (rv == 0) {
38                 printf("No Ethernet devices found\n");
39                 return -ENODEV;
40         }
41         if (bootdev->boot_device_name)
42                 env_set("ethact", bootdev->boot_device_name);
43         rv = net_loop(BOOTP);
44         if (rv < 0) {
45                 printf("Problem booting with BOOTP\n");
46                 return rv;
47         }
48
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;
68 }
69 #endif
70
71 #ifdef CONFIG_SPL_ETH_SUPPORT
72 int spl_net_load_image_cpgmac(struct spl_image_info *spl_image,
73                               struct spl_boot_device *bootdev)
74 {
75 #ifdef CONFIG_SPL_ETH_DEVICE
76         bootdev->boot_device_name = CONFIG_SPL_ETH_DEVICE;
77 #endif
78
79         return spl_net_load_image(spl_image, bootdev);
80 }
81 SPL_LOAD_IMAGE_METHOD("eth device", 0, BOOT_DEVICE_CPGMAC,
82                       spl_net_load_image_cpgmac);
83 #endif
84
85 #ifdef CONFIG_SPL_USB_ETHER
86 int spl_net_load_image_usb(struct spl_image_info *spl_image,
87                            struct spl_boot_device *bootdev)
88 {
89         bootdev->boot_device_name = "usb_ether";
90 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
91         usb_ether_init();
92 #endif
93         return spl_net_load_image(spl_image, bootdev);
94 }
95 SPL_LOAD_IMAGE_METHOD("USB eth", 0, BOOT_DEVICE_USBETH, spl_net_load_image_usb);
96 #endif