sunxi: Fix CPUCFG address for R40
[platform/kernel/u-boot.git] / common / fb_mmc.c
1 /*
2  * Copyright 2014 Broadcom Corporation.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <config.h>
8 #include <common.h>
9 #include <blk.h>
10 #include <fastboot.h>
11 #include <fb_mmc.h>
12 #include <image-sparse.h>
13 #include <part.h>
14 #include <mmc.h>
15 #include <div64.h>
16
17 /*
18  * FIXME: Ensure we always set these names via Kconfig once xxx_PARTITION is
19  * migrated
20  */
21 #ifndef CONFIG_FASTBOOT_GPT_NAME
22 #define CONFIG_FASTBOOT_GPT_NAME "gpt"
23 #endif
24
25
26 #ifndef CONFIG_FASTBOOT_MBR_NAME
27 #define CONFIG_FASTBOOT_MBR_NAME "mbr"
28 #endif
29
30 struct fb_mmc_sparse {
31         struct blk_desc *dev_desc;
32 };
33
34 static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
35                 const char *name, disk_partition_t *info)
36 {
37         int ret;
38
39         ret = part_get_info_by_name(dev_desc, name, info);
40         if (ret) {
41                 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
42                 char env_alias_name[25 + 32 + 1];
43                 char *aliased_part_name;
44
45                 /* check for alias */
46                 strcpy(env_alias_name, "fastboot_partition_alias_");
47                 strncat(env_alias_name, name, 32);
48                 aliased_part_name = getenv(env_alias_name);
49                 if (aliased_part_name != NULL)
50                         ret = part_get_info_by_name(dev_desc,
51                                         aliased_part_name, info);
52         }
53         return ret;
54 }
55
56 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
57                 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
58 {
59         struct fb_mmc_sparse *sparse = info->priv;
60         struct blk_desc *dev_desc = sparse->dev_desc;
61
62         return blk_dwrite(dev_desc, blk, blkcnt, buffer);
63 }
64
65 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
66                 lbaint_t blk, lbaint_t blkcnt)
67 {
68         return blkcnt;
69 }
70
71 static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
72                 const char *part_name, void *buffer,
73                 unsigned int download_bytes)
74 {
75         lbaint_t blkcnt;
76         lbaint_t blks;
77
78         /* determine number of blocks to write */
79         blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
80         blkcnt = lldiv(blkcnt, info->blksz);
81
82         if (blkcnt > info->size) {
83                 error("too large for partition: '%s'\n", part_name);
84                 fastboot_fail("too large for partition");
85                 return;
86         }
87
88         puts("Flashing Raw Image\n");
89
90         blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
91         if (blks != blkcnt) {
92                 error("failed writing to device %d\n", dev_desc->devnum);
93                 fastboot_fail("failed writing to device");
94                 return;
95         }
96
97         printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
98                part_name);
99         fastboot_okay("");
100 }
101
102 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
103                         unsigned int download_bytes)
104 {
105         struct blk_desc *dev_desc;
106         disk_partition_t info;
107
108         dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
109         if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
110                 error("invalid mmc device\n");
111                 fastboot_fail("invalid mmc device");
112                 return;
113         }
114
115 #if CONFIG_IS_ENABLED(EFI_PARTITION)
116         if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
117                 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
118                        __func__);
119                 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
120                         printf("%s: invalid GPT - refusing to write to flash\n",
121                                __func__);
122                         fastboot_fail("invalid GPT partition");
123                         return;
124                 }
125                 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
126                         printf("%s: writing GPT partitions failed\n", __func__);
127                         fastboot_fail("writing GPT partitions failed");
128                         return;
129                 }
130                 printf("........ success\n");
131                 fastboot_okay("");
132                 return;
133         }
134 #endif
135
136 #if CONFIG_IS_ENABLED(DOS_PARTITION)
137         if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
138                 printf("%s: updating MBR\n", __func__);
139                 if (is_valid_dos_buf(download_buffer)) {
140                         printf("%s: invalid MBR - refusing to write to flash\n",
141                                __func__);
142                         fastboot_fail("invalid MBR partition");
143                         return;
144                 }
145                 if (write_mbr_partition(dev_desc, download_buffer)) {
146                         printf("%s: writing MBR partition failed\n", __func__);
147                         fastboot_fail("writing MBR partition failed");
148                         return;
149                 }
150                 printf("........ success\n");
151                 fastboot_okay("");
152                 return;
153         }
154 #endif
155
156         if (part_get_info_by_name_or_alias(dev_desc, cmd, &info)) {
157                 error("cannot find partition: '%s'\n", cmd);
158                 fastboot_fail("cannot find partition");
159                 return;
160         }
161
162         if (is_sparse_image(download_buffer)) {
163                 struct fb_mmc_sparse sparse_priv;
164                 struct sparse_storage sparse;
165
166                 sparse_priv.dev_desc = dev_desc;
167
168                 sparse.blksz = info.blksz;
169                 sparse.start = info.start;
170                 sparse.size = info.size;
171                 sparse.write = fb_mmc_sparse_write;
172                 sparse.reserve = fb_mmc_sparse_reserve;
173
174                 printf("Flashing sparse image at offset " LBAFU "\n",
175                        sparse.start);
176
177                 sparse.priv = &sparse_priv;
178                 write_sparse_image(&sparse, cmd, download_buffer,
179                                    download_bytes);
180         } else {
181                 write_raw_image(dev_desc, &info, cmd, download_buffer,
182                                 download_bytes);
183         }
184 }
185
186 void fb_mmc_erase(const char *cmd)
187 {
188         int ret;
189         struct blk_desc *dev_desc;
190         disk_partition_t info;
191         lbaint_t blks, blks_start, blks_size, grp_size;
192         struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
193
194         if (mmc == NULL) {
195                 error("invalid mmc device");
196                 fastboot_fail("invalid mmc device");
197                 return;
198         }
199
200         dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
201         if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
202                 error("invalid mmc device");
203                 fastboot_fail("invalid mmc device");
204                 return;
205         }
206
207         ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
208         if (ret) {
209                 error("cannot find partition: '%s'", cmd);
210                 fastboot_fail("cannot find partition");
211                 return;
212         }
213
214         /* Align blocks to erase group size to avoid erasing other partitions */
215         grp_size = mmc->erase_grp_size;
216         blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
217         if (info.size >= grp_size)
218                 blks_size = (info.size - (blks_start - info.start)) &
219                                 (~(grp_size - 1));
220         else
221                 blks_size = 0;
222
223         printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
224                blks_start, blks_start + blks_size);
225
226         blks = blk_derase(dev_desc, blks_start, blks_size);
227         if (blks != blks_size) {
228                 error("failed erasing from device %d", dev_desc->devnum);
229                 fastboot_fail("failed erasing from device");
230                 return;
231         }
232
233         printf("........ erased " LBAFU " bytes from '%s'\n",
234                blks_size * info.blksz, cmd);
235         fastboot_okay("");
236 }