common, itest: pass u-boot env variables to itest.s
[kernel/u-boot.git] / common / cmd_disk.c
1 /*
2  * (C) Copyright 2000-2011
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 #include <common.h>
8 #include <command.h>
9 #include <part.h>
10
11 #if defined(CONFIG_CMD_IDE) || defined(CONFIG_CMD_SCSI) || \
12         defined(CONFIG_USB_STORAGE)
13 int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
14                     char *const argv[])
15 {
16         int dev, part;
17         ulong addr = CONFIG_SYS_LOAD_ADDR;
18         ulong cnt;
19         disk_partition_t info;
20         image_header_t *hdr;
21         block_dev_desc_t *dev_desc;
22
23 #if defined(CONFIG_FIT)
24         const void *fit_hdr = NULL;
25 #endif
26
27         bootstage_mark(BOOTSTAGE_ID_IDE_START);
28         if (argc > 3) {
29                 bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
30                 return CMD_RET_USAGE;
31         }
32         bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
33
34         if (argc > 1)
35                 addr = simple_strtoul(argv[1], NULL, 16);
36
37         bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
38
39         part = get_device_and_partition(intf, (argc == 3) ? argv[2] : NULL,
40                                         &dev_desc, &info, 1);
41         if (part < 0) {
42                 bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
43                 return 1;
44         }
45
46         dev = dev_desc->dev;
47         bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
48
49         printf("\nLoading from %s device %d, partition %d: "
50                "Name: %.32s  Type: %.32s\n", intf, dev, part, info.name,
51                info.type);
52
53         debug("First Block: " LBAFU ",  # of blocks: " LBAFU
54               ", Block Size: %ld\n",
55               info.start, info.size, info.blksz);
56
57         if (dev_desc->block_read(dev, info.start, 1, (ulong *) addr) != 1) {
58                 printf("** Read error on %d:%d\n", dev, part);
59                 bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
60                 return 1;
61         }
62         bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
63
64         switch (genimg_get_format((void *) addr)) {
65         case IMAGE_FORMAT_LEGACY:
66                 hdr = (image_header_t *) addr;
67
68                 bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
69
70                 if (!image_check_hcrc(hdr)) {
71                         puts("\n** Bad Header Checksum **\n");
72                         bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
73                         return 1;
74                 }
75                 bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
76
77                 image_print_contents(hdr);
78
79                 cnt = image_get_image_size(hdr);
80                 break;
81 #if defined(CONFIG_FIT)
82         case IMAGE_FORMAT_FIT:
83                 fit_hdr = (const void *) addr;
84                 puts("Fit image detected...\n");
85
86                 cnt = fit_get_size(fit_hdr);
87                 break;
88 #endif
89         default:
90                 bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
91                 puts("** Unknown image type\n");
92                 return 1;
93         }
94
95         cnt += info.blksz - 1;
96         cnt /= info.blksz;
97         cnt -= 1;
98
99         if (dev_desc->block_read(dev, info.start + 1, cnt,
100                                          (ulong *)(addr + info.blksz)) != cnt) {
101                 printf("** Read error on %d:%d\n", dev, part);
102                 bootstage_error(BOOTSTAGE_ID_IDE_READ);
103                 return 1;
104         }
105         bootstage_mark(BOOTSTAGE_ID_IDE_READ);
106
107 #if defined(CONFIG_FIT)
108         /* This cannot be done earlier,
109          * we need complete FIT image in RAM first */
110         if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
111                 if (!fit_check_format(fit_hdr)) {
112                         bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
113                         puts("** Bad FIT image format\n");
114                         return 1;
115                 }
116                 bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
117                 fit_print_contents(fit_hdr);
118         }
119 #endif
120
121         flush_cache(addr, (cnt+1)*info.blksz);
122
123         /* Loading ok, update default load address */
124         load_addr = addr;
125
126         return bootm_maybe_autostart(cmdtp, argv[0]);
127 }
128 #endif