blob: fb06d8a557fb9bc34f4452b537f93f11624ebf4f [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
7#include <common.h>
8#include <fb_mmc.h>
9#include <part.h>
Steve Raec6015672014-08-26 11:47:30 -070010#include <aboot.h>
11#include <sparse_format.h>
Steve Rae792ac482014-08-26 11:47:27 -070012
13/* The 64 defined bytes plus the '\0' */
14#define RESPONSE_LEN (64 + 1)
15
16static char *response_str;
17
Steve Raec6015672014-08-26 11:47:30 -070018void fastboot_fail(const char *s)
Steve Rae792ac482014-08-26 11:47:27 -070019{
Steve Raec6015672014-08-26 11:47:30 -070020 strncpy(response_str, "FAIL", 4);
21 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
Steve Rae792ac482014-08-26 11:47:27 -070022}
23
Steve Raec6015672014-08-26 11:47:30 -070024void fastboot_okay(const char *s)
25{
26 strncpy(response_str, "OKAY", 4);
27 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
28}
29
Steve Rae792ac482014-08-26 11:47:27 -070030static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
31 const char *part_name, void *buffer,
32 unsigned int download_bytes)
33{
34 lbaint_t blkcnt;
35 lbaint_t blks;
36
37 /* determine number of blocks to write */
38 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
39 blkcnt = blkcnt / info->blksz;
40
41 if (blkcnt > info->size) {
42 error("too large for partition: '%s'\n", part_name);
Steve Raec6015672014-08-26 11:47:30 -070043 fastboot_fail("too large for partition");
Steve Rae792ac482014-08-26 11:47:27 -070044 return;
45 }
46
47 puts("Flashing Raw Image\n");
48
49 blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
50 buffer);
51 if (blks != blkcnt) {
52 error("failed writing to device %d\n", dev_desc->dev);
Steve Raec6015672014-08-26 11:47:30 -070053 fastboot_fail("failed writing to device");
Steve Rae792ac482014-08-26 11:47:27 -070054 return;
55 }
56
57 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
58 part_name);
Steve Raec6015672014-08-26 11:47:30 -070059 fastboot_okay("");
Steve Rae792ac482014-08-26 11:47:27 -070060}
61
62void fb_mmc_flash_write(const char *cmd, void *download_buffer,
63 unsigned int download_bytes, char *response)
64{
65 int ret;
66 block_dev_desc_t *dev_desc;
67 disk_partition_t info;
68
69 /* initialize the response buffer */
70 response_str = response;
71
72 dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
73 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
74 error("invalid mmc device\n");
Steve Raec6015672014-08-26 11:47:30 -070075 fastboot_fail("invalid mmc device");
Steve Rae792ac482014-08-26 11:47:27 -070076 return;
77 }
78
79 ret = get_partition_info_efi_by_name(dev_desc, cmd, &info);
80 if (ret) {
81 error("cannot find partition: '%s'\n", cmd);
Steve Raec6015672014-08-26 11:47:30 -070082 fastboot_fail("cannot find partition");
Steve Rae792ac482014-08-26 11:47:27 -070083 return;
84 }
85
Steve Raec6015672014-08-26 11:47:30 -070086 if (is_sparse_image(download_buffer))
87 write_sparse_image(dev_desc, &info, cmd, download_buffer,
88 download_bytes);
89 else
90 write_raw_image(dev_desc, &info, cmd, download_buffer,
91 download_bytes);
Steve Rae792ac482014-08-26 11:47:27 -070092}