blob: 6ea3938d83f29467feb874c8156a200b4641b2a7 [file] [log] [blame]
Steve Rae792ac482014-08-26 11:47:27 -07001/*
2 * Copyright 2014 Broadcom Corporation.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Steve Rae7d059342014-12-12 15:51:54 -08007#include <config.h>
Steve Rae792ac482014-08-26 11:47:27 -07008#include <common.h>
9#include <fb_mmc.h>
10#include <part.h>
Steve Raec6015672014-08-26 11:47:30 -070011#include <aboot.h>
12#include <sparse_format.h>
Steve Rae792ac482014-08-26 11:47:27 -070013
Steve Rae7d059342014-12-12 15:51:54 -080014#ifndef CONFIG_FASTBOOT_GPT_NAME
15#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
16#endif
17
Steve Rae792ac482014-08-26 11:47:27 -070018/* The 64 defined bytes plus the '\0' */
19#define RESPONSE_LEN (64 + 1)
20
21static char *response_str;
22
Steve Raec6015672014-08-26 11:47:30 -070023void fastboot_fail(const char *s)
Steve Rae792ac482014-08-26 11:47:27 -070024{
Steve Raec6015672014-08-26 11:47:30 -070025 strncpy(response_str, "FAIL", 4);
26 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
Steve Rae792ac482014-08-26 11:47:27 -070027}
28
Steve Raec6015672014-08-26 11:47:30 -070029void fastboot_okay(const char *s)
30{
31 strncpy(response_str, "OKAY", 4);
32 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
33}
34
Steve Rae792ac482014-08-26 11:47:27 -070035static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
36 const char *part_name, void *buffer,
37 unsigned int download_bytes)
38{
39 lbaint_t blkcnt;
40 lbaint_t blks;
41
42 /* determine number of blocks to write */
43 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
44 blkcnt = blkcnt / info->blksz;
45
46 if (blkcnt > info->size) {
47 error("too large for partition: '%s'\n", part_name);
Steve Raec6015672014-08-26 11:47:30 -070048 fastboot_fail("too large for partition");
Steve Rae792ac482014-08-26 11:47:27 -070049 return;
50 }
51
52 puts("Flashing Raw Image\n");
53
54 blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
55 buffer);
56 if (blks != blkcnt) {
57 error("failed writing to device %d\n", dev_desc->dev);
Steve Raec6015672014-08-26 11:47:30 -070058 fastboot_fail("failed writing to device");
Steve Rae792ac482014-08-26 11:47:27 -070059 return;
60 }
61
62 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
63 part_name);
Steve Raec6015672014-08-26 11:47:30 -070064 fastboot_okay("");
Steve Rae792ac482014-08-26 11:47:27 -070065}
66
67void fb_mmc_flash_write(const char *cmd, void *download_buffer,
68 unsigned int download_bytes, char *response)
69{
Steve Rae792ac482014-08-26 11:47:27 -070070 block_dev_desc_t *dev_desc;
71 disk_partition_t info;
72
73 /* initialize the response buffer */
74 response_str = response;
75
76 dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
77 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
78 error("invalid mmc device\n");
Steve Raec6015672014-08-26 11:47:30 -070079 fastboot_fail("invalid mmc device");
Steve Rae792ac482014-08-26 11:47:27 -070080 return;
81 }
82
Steve Rae7d059342014-12-12 15:51:54 -080083 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
84 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
85 __func__);
86 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
87 printf("%s: invalid GPT - refusing to write to flash\n",
88 __func__);
89 fastboot_fail("invalid GPT partition");
90 return;
91 }
92 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
93 printf("%s: writing GPT partitions failed\n", __func__);
94 fastboot_fail("writing GPT partitions failed");
95 return;
96 }
97 printf("........ success\n");
98 fastboot_okay("");
99 return;
100 } else if (get_partition_info_efi_by_name(dev_desc, cmd, &info)) {
Steve Rae792ac482014-08-26 11:47:27 -0700101 error("cannot find partition: '%s'\n", cmd);
Steve Raec6015672014-08-26 11:47:30 -0700102 fastboot_fail("cannot find partition");
Steve Rae792ac482014-08-26 11:47:27 -0700103 return;
104 }
105
Steve Raec6015672014-08-26 11:47:30 -0700106 if (is_sparse_image(download_buffer))
107 write_sparse_image(dev_desc, &info, cmd, download_buffer,
108 download_bytes);
109 else
110 write_raw_image(dev_desc, &info, cmd, download_buffer,
111 download_bytes);
Steve Rae792ac482014-08-26 11:47:27 -0700112}