2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-10-15 12:34:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Broadcom Corporation.
|
|
|
|
* Copyright 2015 Free Electrons.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <common.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <blk.h>
|
2020-05-10 17:39:54 +00:00
|
|
|
#include <flash.h>
|
2015-10-15 12:34:17 +00:00
|
|
|
|
|
|
|
#include <fastboot.h>
|
2015-10-15 12:34:19 +00:00
|
|
|
#include <image-sparse.h>
|
2015-10-15 12:34:17 +00:00
|
|
|
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <jffs2/jffs2.h>
|
|
|
|
#include <nand.h>
|
|
|
|
|
|
|
|
struct fb_nand_sparse {
|
2016-06-07 18:19:36 +00:00
|
|
|
struct mtd_info *mtd;
|
2015-10-15 12:34:17 +00:00
|
|
|
struct part_info *part;
|
|
|
|
};
|
|
|
|
|
2015-10-15 12:34:18 +00:00
|
|
|
__weak int board_fastboot_erase_partition_setup(char *name)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__weak int board_fastboot_write_partition_setup(char *name)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-07 18:19:37 +00:00
|
|
|
static int fb_nand_lookup(const char *partname,
|
2016-06-07 21:22:59 +00:00
|
|
|
struct mtd_info **mtd,
|
2018-05-29 15:30:40 +00:00
|
|
|
struct part_info **part,
|
|
|
|
char *response)
|
2015-10-15 12:34:17 +00:00
|
|
|
{
|
|
|
|
struct mtd_device *dev;
|
|
|
|
int ret;
|
|
|
|
u8 pnum;
|
|
|
|
|
|
|
|
ret = mtdparts_init();
|
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("Cannot initialize MTD partitions\n");
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_fail("cannot init mtdparts", response);
|
2015-10-15 12:34:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = find_dev_and_part(partname, &dev, &pnum, part);
|
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("cannot find partition: '%s'", partname);
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_fail("cannot find partition", response);
|
2015-10-15 12:34:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->id->type != MTD_DEV_TYPE_NAND) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("partition '%s' is not stored on a NAND device",
|
2015-10-15 12:34:17 +00:00
|
|
|
partname);
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_fail("not a NAND device", response);
|
2015-10-15 12:34:17 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-06-27 00:12:56 +00:00
|
|
|
*mtd = get_nand_dev_by_index(dev->id->num);
|
2015-10-15 12:34:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-30 18:57:54 +00:00
|
|
|
static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
|
2015-10-15 12:34:17 +00:00
|
|
|
{
|
|
|
|
nand_erase_options_t opts;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
opts.offset = part->offset;
|
|
|
|
opts.length = part->size;
|
|
|
|
opts.quiet = 1;
|
|
|
|
|
|
|
|
printf("Erasing blocks 0x%llx to 0x%llx\n",
|
|
|
|
part->offset, part->offset + part->size);
|
|
|
|
|
2016-05-30 18:57:54 +00:00
|
|
|
ret = nand_erase_opts(mtd, &opts);
|
2015-10-15 12:34:17 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
printf("........ erased 0x%llx bytes from '%s'\n",
|
|
|
|
part->size, part->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-30 18:57:54 +00:00
|
|
|
static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
|
2018-05-29 15:30:53 +00:00
|
|
|
void *buffer, u32 offset,
|
2018-05-29 15:30:45 +00:00
|
|
|
size_t length, size_t *written)
|
2015-10-15 12:34:17 +00:00
|
|
|
{
|
|
|
|
int flags = WITH_WR_VERIFY;
|
|
|
|
|
|
|
|
#ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
|
|
|
|
flags |= WITH_DROP_FFS;
|
|
|
|
#endif
|
|
|
|
|
2016-05-30 18:57:54 +00:00
|
|
|
return nand_write_skip_bad(mtd, offset, &length, written,
|
2015-10-15 12:34:17 +00:00
|
|
|
part->size - (offset - part->offset),
|
|
|
|
buffer, flags);
|
|
|
|
}
|
|
|
|
|
2016-06-07 18:19:36 +00:00
|
|
|
static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
|
|
|
|
lbaint_t blk, lbaint_t blkcnt, const void *buffer)
|
2015-10-15 12:34:17 +00:00
|
|
|
{
|
2016-06-07 18:19:36 +00:00
|
|
|
struct fb_nand_sparse *sparse = info->priv;
|
2015-10-15 12:34:17 +00:00
|
|
|
size_t written;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-07 18:19:36 +00:00
|
|
|
ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
|
|
|
|
blk * info->blksz,
|
|
|
|
blkcnt * info->blksz, &written);
|
2015-10-15 12:34:17 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
printf("Failed to write sparse chunk\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-07 18:19:36 +00:00
|
|
|
/* TODO - verify that the value "written" includes the "bad-blocks" ... */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the return value must be 'blkcnt' ("good-blocks") plus the
|
|
|
|
* number of "bad-blocks" encountered within this space...
|
|
|
|
*/
|
|
|
|
return written / info->blksz;
|
2015-10-15 12:34:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 18:19:38 +00:00
|
|
|
static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
|
|
|
|
lbaint_t blk, lbaint_t blkcnt)
|
|
|
|
{
|
|
|
|
int bad_blocks = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO - implement a function to determine the total number
|
|
|
|
* of blocks which must be used in order to reserve the specified
|
|
|
|
* number ("blkcnt") of "good-blocks", starting at "blk"...
|
|
|
|
* ( possibly something like the "check_skip_len()" function )
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the return value must be 'blkcnt' ("good-blocks") plus the
|
|
|
|
* number of "bad-blocks" encountered within this space...
|
|
|
|
*/
|
|
|
|
return blkcnt + bad_blocks;
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:30:53 +00:00
|
|
|
/**
|
|
|
|
* fastboot_nand_get_part_info() - Lookup NAND partion by name
|
|
|
|
*
|
|
|
|
* @part_name: Named device to lookup
|
|
|
|
* @part_info: Pointer to returned part_info pointer
|
|
|
|
* @response: Pointer to fastboot response buffer
|
|
|
|
*/
|
2019-06-13 18:11:07 +00:00
|
|
|
int fastboot_nand_get_part_info(const char *part_name,
|
|
|
|
struct part_info **part_info, char *response)
|
2018-05-29 15:30:53 +00:00
|
|
|
{
|
|
|
|
struct mtd_info *mtd = NULL;
|
|
|
|
|
|
|
|
return fb_nand_lookup(part_name, &mtd, part_info, response);
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:30:48 +00:00
|
|
|
/**
|
|
|
|
* fastboot_nand_flash_write() - Write image to NAND for fastboot
|
|
|
|
*
|
|
|
|
* @cmd: Named device to write image to
|
|
|
|
* @download_buffer: Pointer to image data
|
|
|
|
* @download_bytes: Size of image data
|
|
|
|
* @response: Pointer to fastboot response buffer
|
|
|
|
*/
|
|
|
|
void fastboot_nand_flash_write(const char *cmd, void *download_buffer,
|
2018-05-29 15:30:53 +00:00
|
|
|
u32 download_bytes, char *response)
|
2015-10-15 12:34:17 +00:00
|
|
|
{
|
|
|
|
struct part_info *part;
|
2016-05-30 18:57:54 +00:00
|
|
|
struct mtd_info *mtd = NULL;
|
2015-10-15 12:34:17 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-05-29 15:30:40 +00:00
|
|
|
ret = fb_nand_lookup(cmd, &mtd, &part, response);
|
2015-10-15 12:34:17 +00:00
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("invalid NAND device");
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_fail("invalid NAND device", response);
|
2015-10-15 12:34:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:18 +00:00
|
|
|
ret = board_fastboot_write_partition_setup(part->name);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
|
2015-10-15 12:34:17 +00:00
|
|
|
if (is_sparse_image(download_buffer)) {
|
|
|
|
struct fb_nand_sparse sparse_priv;
|
2016-06-07 18:19:36 +00:00
|
|
|
struct sparse_storage sparse;
|
2015-10-15 12:34:17 +00:00
|
|
|
|
2016-06-07 18:19:36 +00:00
|
|
|
sparse_priv.mtd = mtd;
|
2015-10-15 12:34:17 +00:00
|
|
|
sparse_priv.part = part;
|
|
|
|
|
2016-06-07 18:19:36 +00:00
|
|
|
sparse.blksz = mtd->writesize;
|
|
|
|
sparse.start = part->offset / sparse.blksz;
|
|
|
|
sparse.size = part->size / sparse.blksz;
|
2015-10-15 12:34:17 +00:00
|
|
|
sparse.write = fb_nand_sparse_write;
|
2016-06-07 18:19:38 +00:00
|
|
|
sparse.reserve = fb_nand_sparse_reserve;
|
2018-04-06 06:35:09 +00:00
|
|
|
sparse.mssg = fastboot_fail;
|
2015-10-15 12:34:17 +00:00
|
|
|
|
2016-06-07 18:19:36 +00:00
|
|
|
printf("Flashing sparse image at offset " LBAFU "\n",
|
|
|
|
sparse.start);
|
|
|
|
|
|
|
|
sparse.priv = &sparse_priv;
|
2018-05-29 15:30:40 +00:00
|
|
|
ret = write_sparse_image(&sparse, cmd, download_buffer,
|
|
|
|
response);
|
2018-04-06 06:35:09 +00:00
|
|
|
if (!ret)
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_okay(NULL, response);
|
2015-10-15 12:34:17 +00:00
|
|
|
} else {
|
|
|
|
printf("Flashing raw image at offset 0x%llx\n",
|
|
|
|
part->offset);
|
|
|
|
|
2016-05-30 18:57:54 +00:00
|
|
|
ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
|
2015-10-15 12:34:17 +00:00
|
|
|
download_bytes, NULL);
|
|
|
|
|
|
|
|
printf("........ wrote %u bytes to '%s'\n",
|
|
|
|
download_bytes, part->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_fail("error writing the image", response);
|
2015-10-15 12:34:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_okay(NULL, response);
|
2015-10-15 12:34:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 15:30:48 +00:00
|
|
|
/**
|
|
|
|
* fastboot_nand_flash_erase() - Erase NAND for fastboot
|
|
|
|
*
|
|
|
|
* @cmd: Named device to erase
|
|
|
|
* @response: Pointer to fastboot response buffer
|
|
|
|
*/
|
|
|
|
void fastboot_nand_erase(const char *cmd, char *response)
|
2015-10-15 12:34:17 +00:00
|
|
|
{
|
|
|
|
struct part_info *part;
|
2016-05-30 18:57:54 +00:00
|
|
|
struct mtd_info *mtd = NULL;
|
2015-10-15 12:34:17 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-05-29 15:30:40 +00:00
|
|
|
ret = fb_nand_lookup(cmd, &mtd, &part, response);
|
2015-10-15 12:34:17 +00:00
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("invalid NAND device");
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_fail("invalid NAND device", response);
|
2015-10-15 12:34:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:18 +00:00
|
|
|
ret = board_fastboot_erase_partition_setup(part->name);
|
|
|
|
if (ret)
|
|
|
|
return;
|
|
|
|
|
2016-05-30 18:57:54 +00:00
|
|
|
ret = _fb_nand_erase(mtd, part);
|
2015-10-15 12:34:17 +00:00
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("failed erasing from device %s", mtd->name);
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_fail("failed erasing from device", response);
|
2015-10-15 12:34:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:30:40 +00:00
|
|
|
fastboot_okay(NULL, response);
|
2015-10-15 12:34:17 +00:00
|
|
|
}
|