summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile1
-rw-r--r--tools/env/fw_env.c77
-rw-r--r--tools/env/fw_env.h1
-rw-r--r--tools/env/fw_env_main.c35
-rw-r--r--tools/fdtgrep.c2
-rw-r--r--tools/vybridimage.c164
6 files changed, 233 insertions, 47 deletions
diff --git a/tools/Makefile b/tools/Makefile
index 421414bc154..e6f7993f995 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -89,6 +89,7 @@ dumpimage-mkimage-objs := aisimage.o \
os_support.o \
pblimage.o \
pbl_crc32.o \
+ vybridimage.o \
$(ROCKCHIP_OBS) \
socfpgaimage.o \
lib/sha1.o \
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index d27f57e251f..3dc0d5344cc 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -643,16 +643,28 @@ int fw_parse_script(char *fname, struct env_opts *opts)
return ret;
}
+/**
+ * environment_end() - compute offset of first byte right after environemnt
+ * @dev - index of enviroment buffer
+ * Return:
+ * device offset of first byte right after environemnt
+ */
+off_t environment_end(int dev)
+{
+ /* environment is block aligned */
+ return DEVOFFSET(dev) + ENVSECTORS(dev) * DEVESIZE(dev);
+}
+
/*
* Test for bad block on NAND, just returns 0 on NOR, on NAND:
* 0 - block is good
* > 0 - block is bad
* < 0 - failed to test
*/
-static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
+static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
{
if (mtd_type == MTD_NANDFLASH) {
- int badblock = ioctl (fd, MEMGETBADBLOCK, blockstart);
+ int badblock = ioctl(fd, MEMGETBADBLOCK, &blockstart);
if (badblock < 0) {
perror ("Cannot read bad block mark");
@@ -662,7 +674,7 @@ static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
if (badblock) {
#ifdef DEBUG
fprintf (stderr, "Bad block at 0x%llx, skipping\n",
- (unsigned long long) *blockstart);
+ (unsigned long long)blockstart);
#endif
return badblock;
}
@@ -677,13 +689,12 @@ static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
* the DEVOFFSET (dev) block. On NOR the loop is only run once.
*/
static int flash_read_buf (int dev, int fd, void *buf, size_t count,
- off_t offset, uint8_t mtd_type)
+ off_t offset)
{
size_t blocklen; /* erase / write length - one block on NAND,
0 on NOR */
size_t processed = 0; /* progress counter */
size_t readlen = count; /* current read length */
- off_t top_of_range; /* end of the last block we may use */
off_t block_seek; /* offset inside the current block to the start
of the data */
loff_t blockstart; /* running start of the current block -
@@ -695,35 +706,27 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
/* Offset inside a block */
block_seek = offset - blockstart;
- if (mtd_type == MTD_NANDFLASH) {
+ if (DEVTYPE(dev) == MTD_NANDFLASH) {
/*
* NAND: calculate which blocks we are reading. We have
* to read one block at a time to skip bad blocks.
*/
blocklen = DEVESIZE (dev);
- /*
- * To calculate the top of the range, we have to use the
- * global DEVOFFSET (dev), which can be different from offset
- */
- top_of_range = ((DEVOFFSET(dev) / blocklen) +
- ENVSECTORS (dev)) * blocklen;
-
/* Limit to one block for the first read */
if (readlen > blocklen - block_seek)
readlen = blocklen - block_seek;
} else {
blocklen = 0;
- top_of_range = offset + count;
}
/* This only runs once on NOR flash */
while (processed < count) {
- rc = flash_bad_block (fd, mtd_type, &blockstart);
+ rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
if (rc < 0) /* block test failed */
return -1;
- if (blockstart + block_seek + readlen > top_of_range) {
+ if (blockstart + block_seek + readlen > environment_end(dev)) {
/* End of range is reached */
fprintf (stderr,
"Too few good blocks within range\n");
@@ -762,12 +765,12 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
}
/*
- * Write count bytes at offset, but stay within ENVSECTORS (dev) sectors of
+ * Write count bytes from begin of environment, but stay within
+ * ENVSECTORS(dev) sectors of
* DEVOFFSET (dev). Similar to the read case above, on NOR and dataflash we
* erase and write the whole data at once.
*/
-static int flash_write_buf (int dev, int fd, void *buf, size_t count,
- off_t offset, uint8_t mtd_type)
+static int flash_write_buf(int dev, int fd, void *buf, size_t count)
{
void *data;
struct erase_info_user erase;
@@ -783,7 +786,6 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
below offset */
off_t block_seek; /* offset inside the erase block to the start
of the data */
- off_t top_of_range; /* end of the last block we may use */
loff_t blockstart; /* running start of the current block -
MEMGETBADBLOCK needs 64 bits */
int rc;
@@ -791,27 +793,24 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
/*
* For mtd devices only offset and size of the environment do matter
*/
- if (mtd_type == MTD_ABSENT) {
+ if (DEVTYPE(dev) == MTD_ABSENT) {
blocklen = count;
- top_of_range = offset + count;
erase_len = blocklen;
- blockstart = offset;
+ blockstart = DEVOFFSET(dev);
block_seek = 0;
write_total = blocklen;
} else {
blocklen = DEVESIZE(dev);
- top_of_range = ((DEVOFFSET(dev) / blocklen) +
- ENVSECTORS(dev)) * blocklen;
-
- erase_offset = (offset / blocklen) * blocklen;
+ erase_offset = DEVOFFSET(dev);
/* Maximum area we may use */
- erase_len = top_of_range - erase_offset;
+ erase_len = environment_end(dev) - erase_offset;
blockstart = erase_offset;
+
/* Offset inside a block */
- block_seek = offset - erase_offset;
+ block_seek = DEVOFFSET(dev) - erase_offset;
/*
* Data size we actually write: from the start of the block
@@ -836,8 +835,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
return -1;
}
- rc = flash_read_buf (dev, fd, data, write_total, erase_offset,
- mtd_type);
+ rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
if (write_total != rc)
return -1;
@@ -864,7 +862,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
data = buf;
}
- if (mtd_type == MTD_NANDFLASH) {
+ if (DEVTYPE(dev) == MTD_NANDFLASH) {
/*
* NAND: calculate which blocks we are writing. We have
* to write one block at a time to skip bad blocks.
@@ -878,11 +876,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
/* This only runs once on NOR flash and SPI-dataflash */
while (processed < write_total) {
- rc = flash_bad_block (fd, mtd_type, &blockstart);
+ rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
if (rc < 0) /* block test failed */
return rc;
- if (blockstart + erasesize > top_of_range) {
+ if (blockstart + erasesize > environment_end(dev)) {
fprintf (stderr, "End of range reached, aborting\n");
return -1;
}
@@ -892,11 +890,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
continue;
}
- if (mtd_type != MTD_ABSENT) {
+ if (DEVTYPE(dev) != MTD_ABSENT) {
erase.start = blockstart;
ioctl(fd, MEMUNLOCK, &erase);
/* These do not need an explicit erase cycle */
- if (mtd_type != MTD_DATAFLASH)
+ if (DEVTYPE(dev) != MTD_DATAFLASH)
if (ioctl(fd, MEMERASE, &erase) != 0) {
fprintf(stderr,
"MTD erase error on %s: %s\n",
@@ -923,7 +921,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
return -1;
}
- if (mtd_type != MTD_ABSENT)
+ if (DEVTYPE(dev) != MTD_ABSENT)
ioctl(fd, MEMLOCK, &erase);
processed += erasesize;
@@ -1010,8 +1008,7 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
#endif
rc = flash_write_buf(dev_target, fd_target, environment.image,
- CUR_ENVSIZE, DEVOFFSET(dev_target),
- DEVTYPE(dev_target));
+ CUR_ENVSIZE);
if (rc < 0)
return rc;
@@ -1035,7 +1032,7 @@ static int flash_read (int fd)
int rc;
rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
- DEVOFFSET(dev_current), DEVTYPE(dev_current));
+ DEVOFFSET(dev_current));
if (rc != CUR_ENVSIZE)
return -1;
diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h
index 436eca9ddc1..05588ab6d55 100644
--- a/tools/env/fw_env.h
+++ b/tools/env/fw_env.h
@@ -63,6 +63,7 @@ struct env_opts {
#endif
int aes_flag; /* Is AES encryption used? */
uint8_t aes_key[AES_KEY_LENGTH];
+ char *lockname;
};
int parse_aes_key(char *key, uint8_t *bin_key);
diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
index 7a17b28f40b..443de36e437 100644
--- a/tools/env/fw_env_main.c
+++ b/tools/env/fw_env_main.c
@@ -46,6 +46,7 @@ static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"script", required_argument, NULL, 's'},
{"noheader", required_argument, NULL, 'n'},
+ {"lock", required_argument, NULL, 'l'},
{NULL, 0, NULL, 0}
};
@@ -72,6 +73,7 @@ void usage_printenv(void)
" -c, --config configuration file, default:" CONFIG_FILE "\n"
#endif
" -n, --noheader do not repeat variable name in output\n"
+ " -l, --lock lock node, default:/var/lock\n"
"\n");
}
@@ -88,6 +90,7 @@ void usage_setenv(void)
#ifdef CONFIG_FILE
" -c, --config configuration file, default:" CONFIG_FILE "\n"
#endif
+ " -l, --lock lock node, default:/var/lock\n"
" -s, --script batch mode to minimize writes\n"
"\n"
"Examples:\n"
@@ -119,7 +122,7 @@ static void parse_common_args(int argc, char *argv[])
env_opts.config_file = CONFIG_FILE;
#endif
- while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
+ while ((c = getopt_long(argc, argv, ":a:c:l:h", long_options, NULL)) !=
EOF) {
switch (c) {
case 'a':
@@ -134,6 +137,9 @@ static void parse_common_args(int argc, char *argv[])
env_opts.config_file = optarg;
break;
#endif
+ case 'l':
+ env_opts.lockname = optarg;
+ break;
case 'h':
do_printenv ? usage_printenv() : usage_setenv();
exit(EXIT_SUCCESS);
@@ -155,8 +161,8 @@ int parse_printenv_args(int argc, char *argv[])
parse_common_args(argc, argv);
- while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
- EOF) {
+ while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL))
+ != EOF) {
switch (c) {
case 'n':
noheader = 1;
@@ -164,6 +170,7 @@ int parse_printenv_args(int argc, char *argv[])
case 'a':
case 'c':
case 'h':
+ case 'l':
/* ignore common options */
break;
default: /* '?' */
@@ -181,8 +188,8 @@ int parse_setenv_args(int argc, char *argv[])
parse_common_args(argc, argv);
- while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
- EOF) {
+ while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL))
+ != EOF) {
switch (c) {
case 's':
script_file = optarg;
@@ -190,6 +197,7 @@ int parse_setenv_args(int argc, char *argv[])
case 'a':
case 'c':
case 'h':
+ case 'l':
/* ignore common options */
break;
default: /* '?' */
@@ -203,7 +211,7 @@ int parse_setenv_args(int argc, char *argv[])
int main(int argc, char *argv[])
{
- const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
+ char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
int lockfd = -1;
int retval = EXIT_SUCCESS;
char *_cmdname;
@@ -235,6 +243,18 @@ int main(int argc, char *argv[])
argc -= optind;
argv += optind;
+ if (env_opts.lockname) {
+ lockname = malloc(sizeof(env_opts.lockname) +
+ sizeof(CMD_PRINTENV) + 10);
+ if (!lockname) {
+ fprintf(stderr, "Unable allocate memory");
+ exit(EXIT_FAILURE);
+ }
+
+ sprintf(lockname, "%s/%s.lock",
+ env_opts.lockname, CMD_PRINTENV);
+ }
+
lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (-1 == lockfd) {
fprintf(stderr, "Error opening lock file %s\n", lockname);
@@ -260,6 +280,9 @@ int main(int argc, char *argv[])
}
}
+ if (env_opts.lockname)
+ free(lockname);
+
flock(lockfd, LOCK_UN);
close(lockfd);
return retval;
diff --git a/tools/fdtgrep.c b/tools/fdtgrep.c
index 8d3fef40279..b9078273c95 100644
--- a/tools/fdtgrep.c
+++ b/tools/fdtgrep.c
@@ -405,7 +405,7 @@ static int display_fdt_by_regions(struct display_info *disp, const void *blob,
* The output of this function may or may not be a valid FDT. To ensure it
* is, these disp->flags must be set:
*
- * FDT_REG_SUPERNODES: ensures that subnodes are preceeded by their
+ * FDT_REG_SUPERNODES: ensures that subnodes are preceded by their
* parents. Without this option, fragments of subnode data may be
* output without the supernodes above them. This is useful for
* hashing but cannot produce a valid FDT.
diff --git a/tools/vybridimage.c b/tools/vybridimage.c
new file mode 100644
index 00000000000..a31fc1099cb
--- /dev/null
+++ b/tools/vybridimage.c
@@ -0,0 +1,164 @@
+/*
+ * Image manipulator for Vybrid SoCs
+ *
+ * Derived from vybridimage.c
+ *
+ * (C) Copyright 2016 DENX Software Engineering GmbH
+ * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "imagetool.h"
+#include <compiler.h>
+#include <image.h>
+
+/*
+ * NAND page 0 boot header
+ */
+
+struct nand_page_0_boot_header {
+ union {
+ uint32_t fcb[128];
+ uint8_t fcb_bytes[512];
+ }; /* 0x00000000 - 0x000001ff */
+ uint8_t sw_ecc[512]; /* 0x00000200 - 0x000003ff */
+ uint32_t padding[65280]; /* 0x00000400 - 0x0003ffff */
+ uint8_t ivt_prefix[1024]; /* 0x00040000 - 0x000403ff */
+};
+
+/* signature byte for a readable block */
+
+static struct nand_page_0_boot_header vybridimage_header;
+
+static int vybridimage_check_image_types(uint8_t type)
+{
+ if (type == IH_TYPE_VYBRIDIMAGE)
+ return EXIT_SUCCESS;
+ return EXIT_FAILURE;
+}
+
+static uint8_t vybridimage_sw_ecc(uint8_t byte)
+{
+ uint8_t bit0 = (byte & (1 << 0)) ? 1 : 0;
+ uint8_t bit1 = (byte & (1 << 1)) ? 1 : 0;
+ uint8_t bit2 = (byte & (1 << 2)) ? 1 : 0;
+ uint8_t bit3 = (byte & (1 << 3)) ? 1 : 0;
+ uint8_t bit4 = (byte & (1 << 4)) ? 1 : 0;
+ uint8_t bit5 = (byte & (1 << 5)) ? 1 : 0;
+ uint8_t bit6 = (byte & (1 << 6)) ? 1 : 0;
+ uint8_t bit7 = (byte & (1 << 7)) ? 1 : 0;
+ uint8_t res = 0;
+
+ res |= ((bit6 ^ bit5 ^ bit3 ^ bit2) << 0);
+ res |= ((bit7 ^ bit5 ^ bit4 ^ bit2 ^ bit1) << 1);
+ res |= ((bit7 ^ bit6 ^ bit5 ^ bit1 ^ bit0) << 2);
+ res |= ((bit7 ^ bit4 ^ bit3 ^ bit0) << 3);
+ res |= ((bit6 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0) << 4);
+
+ return res;
+}
+
+static int vybridimage_verify_header(unsigned char *ptr, int image_size,
+ struct image_tool_params *params)
+{
+ struct nand_page_0_boot_header *hdr =
+ (struct nand_page_0_boot_header *)ptr;
+ int idx;
+
+ if (hdr->fcb[1] != 0x46434220)
+ return -1;
+ if (hdr->fcb[2] != 1)
+ return -1;
+ if (hdr->fcb[7] != 64)
+ return -1;
+ if (hdr->fcb[14] != 6)
+ return -1;
+ if (hdr->fcb[30] != 0x0001ff00)
+ return -1;
+ if (hdr->fcb[43] != 1)
+ return -1;
+ if (hdr->fcb[54] != 0)
+ return -1;
+ if (hdr->fcb[55] != 8)
+ return -1;
+
+ /* check software ECC */
+ for (idx = 0; idx < sizeof(hdr->fcb_bytes); idx++) {
+ uint8_t sw_ecc = vybridimage_sw_ecc(hdr->fcb_bytes[idx]);
+ if (sw_ecc != hdr->sw_ecc[idx])
+ return -1;
+ }
+
+ return 0;
+}
+
+static void vybridimage_set_header(void *ptr, struct stat *sbuf, int ifd,
+ struct image_tool_params *params)
+{
+ struct nand_page_0_boot_header *hdr =
+ (struct nand_page_0_boot_header *)ptr;
+ int idx;
+
+ /* fill header with 0x00 for first 56 entries then 0xff */
+ memset(&hdr->fcb[0], 0x0, 56*sizeof(uint32_t));
+ memset(&hdr->fcb[56], 0xff, 72*sizeof(uint32_t));
+ /* fill SW ecc and padding with 0xff */
+ memset(&hdr->sw_ecc[0], 0xff, sizeof(hdr->sw_ecc));
+ memset(&hdr->padding[0], 0xff, sizeof(hdr->padding));
+ /* fill IVT prefix with 0x00 */
+ memset(&hdr->ivt_prefix[0], 0x00, sizeof(hdr->ivt_prefix));
+
+ /* populate fcb */
+ hdr->fcb[1] = 0x46434220; /* signature */
+ hdr->fcb[2] = 0x00000001; /* version */
+ hdr->fcb[5] = 2048; /* page size */
+ hdr->fcb[6] = (2048+64); /* page + OOB size */
+ hdr->fcb[7] = 64; /* pages per block */
+ hdr->fcb[14] = 6; /* ECC mode 6 */
+ hdr->fcb[26] = 128; /* fw address (0x40000) in 2K pages */
+ hdr->fcb[27] = 128; /* fw address (0x40000) in 2K pages */
+ hdr->fcb[30] = 0x0001ff00; /* DBBT search area start address */
+ hdr->fcb[33] = 2048; /* BB marker physical offset */
+ hdr->fcb[43] = 1; /* DISBBM */
+ hdr->fcb[54] = 0; /* DISBB_Search */
+ hdr->fcb[55] = 8; /* Bad block search limit */
+
+ /* compute software ECC */
+ for (idx = 0; idx < sizeof(hdr->fcb_bytes); idx++)
+ hdr->sw_ecc[idx] = vybridimage_sw_ecc(hdr->fcb_bytes[idx]);
+}
+
+static void vybridimage_print_hdr_field(struct nand_page_0_boot_header *hdr,
+ int idx)
+{
+ printf("header.fcb[%d] = %08x\n", idx, hdr->fcb[idx]);
+}
+
+static void vybridimage_print_header(const void *ptr)
+{
+ struct nand_page_0_boot_header *hdr =
+ (struct nand_page_0_boot_header *)ptr;
+ int idx;
+
+ for (idx = 0; idx < 56; idx++)
+ vybridimage_print_hdr_field(hdr, idx);
+}
+
+/*
+ * vybridimage parameters
+ */
+U_BOOT_IMAGE_TYPE(
+ vybridimage,
+ "Vybrid Boot Image",
+ sizeof(vybridimage_header),
+ (void *)&vybridimage_header,
+ NULL,
+ vybridimage_verify_header,
+ vybridimage_print_header,
+ vybridimage_set_header,
+ NULL,
+ vybridimage_check_image_types,
+ NULL,
+ NULL
+);