common: block: fix compiler error with CONFIG_FASTBOOT_FLASH_MMC_DEV
[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 #ifndef CONFIG_FASTBOOT_GPT_NAME
18 #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
19 #endif
20
21 struct fb_mmc_sparse {
22         struct blk_desc *dev_desc;
23 };
24
25 static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
26                 const char *name, disk_partition_t *info)
27 {
28         int ret;
29
30         ret = part_get_info_efi_by_name(dev_desc, name, info);
31         if (ret) {
32                 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
33                 char env_alias_name[25 + 32 + 1];
34                 char *aliased_part_name;
35
36                 /* check for alias */
37                 strcpy(env_alias_name, "fastboot_partition_alias_");
38                 strncat(env_alias_name, name, 32);
39                 aliased_part_name = getenv(env_alias_name);
40                 if (aliased_part_name != NULL)
41                         ret = part_get_info_efi_by_name(dev_desc,
42                                         aliased_part_name, info);
43         }
44         return ret;
45 }
46
47 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
48                 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
49 {
50         struct fb_mmc_sparse *sparse = info->priv;
51         struct blk_desc *dev_desc = sparse->dev_desc;
52
53         return blk_dwrite(dev_desc, blk, blkcnt, buffer);
54 }
55
56 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
57                 lbaint_t blk, lbaint_t blkcnt)
58 {
59         return blkcnt;
60 }
61
62 static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
63                 const char *part_name, void *buffer,
64                 unsigned int download_bytes)
65 {
66         lbaint_t blkcnt;
67         lbaint_t blks;
68
69         /* determine number of blocks to write */
70         blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
71         blkcnt = lldiv(blkcnt, info->blksz);
72
73         if (blkcnt > info->size) {
74                 error("too large for partition: '%s'\n", part_name);
75                 fastboot_fail("too large for partition");
76                 return;
77         }
78
79         puts("Flashing Raw Image\n");
80
81         blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
82         if (blks != blkcnt) {
83                 error("failed writing to device %d\n", dev_desc->devnum);
84                 fastboot_fail("failed writing to device");
85                 return;
86         }
87
88         printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
89                part_name);
90         fastboot_okay("");
91 }
92
93 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
94                         unsigned int download_bytes)
95 {
96         struct blk_desc *dev_desc;
97         disk_partition_t info;
98
99         dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
100         if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
101                 error("invalid mmc device\n");
102                 fastboot_fail("invalid mmc device");
103                 return;
104         }
105
106         if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
107                 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
108                        __func__);
109                 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
110                         printf("%s: invalid GPT - refusing to write to flash\n",
111                                __func__);
112                         fastboot_fail("invalid GPT partition");
113                         return;
114                 }
115                 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
116                         printf("%s: writing GPT partitions failed\n", __func__);
117                         fastboot_fail(
118                                       "writing GPT partitions failed");
119                         return;
120                 }
121                 printf("........ success\n");
122                 fastboot_okay("");
123                 return;
124         } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
125                 error("cannot find partition: '%s'\n", cmd);
126                 fastboot_fail("cannot find partition");
127                 return;
128         }
129
130         if (is_sparse_image(download_buffer)) {
131                 struct fb_mmc_sparse sparse_priv;
132                 struct sparse_storage sparse;
133
134                 sparse_priv.dev_desc = dev_desc;
135
136                 sparse.blksz = info.blksz;
137                 sparse.start = info.start;
138                 sparse.size = info.size;
139                 sparse.write = fb_mmc_sparse_write;
140                 sparse.reserve = fb_mmc_sparse_reserve;
141
142                 printf("Flashing sparse image at offset " LBAFU "\n",
143                        sparse.start);
144
145                 sparse.priv = &sparse_priv;
146                 write_sparse_image(&sparse, cmd, download_buffer,
147                                    download_bytes);
148         } else {
149                 write_raw_image(dev_desc, &info, cmd, download_buffer,
150                                 download_bytes);
151         }
152 }
153
154 void fb_mmc_erase(const char *cmd)
155 {
156         int ret;
157         struct blk_desc *dev_desc;
158         disk_partition_t info;
159         lbaint_t blks, blks_start, blks_size, grp_size;
160         struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
161
162         if (mmc == NULL) {
163                 error("invalid mmc device");
164                 fastboot_fail("invalid mmc device");
165                 return;
166         }
167
168         dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
169         if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
170                 error("invalid mmc device");
171                 fastboot_fail("invalid mmc device");
172                 return;
173         }
174
175         ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
176         if (ret) {
177                 error("cannot find partition: '%s'", cmd);
178                 fastboot_fail("cannot find partition");
179                 return;
180         }
181
182         /* Align blocks to erase group size to avoid erasing other partitions */
183         grp_size = mmc->erase_grp_size;
184         blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
185         if (info.size >= grp_size)
186                 blks_size = (info.size - (blks_start - info.start)) &
187                                 (~(grp_size - 1));
188         else
189                 blks_size = 0;
190
191         printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
192                blks_start, blks_start + blks_size);
193
194         blks = blk_derase(dev_desc, blks_start, blks_size);
195         if (blks != blks_size) {
196                 error("failed erasing from device %d", dev_desc->devnum);
197                 fastboot_fail("failed erasing from device");
198                 return;
199         }
200
201         printf("........ erased " LBAFU " bytes from '%s'\n",
202                blks_size * info.blksz, cmd);
203         fastboot_okay("");
204 }