[U-Boot] [PATCH v2 2/4] tools: mkimage (type=kwbimage) kirkwood boot image support
Prafulla Wadaskar
prafulla at marvell.com
Tue Jul 21 23:25:49 CEST 2009
For more details refer docs/README.kwbimage
Signed-off-by: Prafulla Wadaskar <prafulla at marvell.com>
---
Change log:
v2: strtok_r() used for simple parser algorithm
updated as per feedback for v1
all line checked to be below 78 :-)
Makefile | 5 +
common/image.c | 1 +
doc/README.kwbimage | 93 ++++++++++++++++++
include/image.h | 1 +
tools/Makefile | 1 +
tools/kwbimage.c | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++
tools/kwbimage.h | 104 ++++++++++++++++++++
tools/mkimage.c | 26 +++++-
8 files changed, 494 insertions(+), 1 deletions(-)
create mode 100644 doc/README.kwbimage
create mode 100644 tools/kwbimage.c
create mode 100644 tools/kwbimage.h
diff --git a/Makefile b/Makefile
index 55fc2a3..ebe00dc 100644
--- a/Makefile
+++ b/Makefile
@@ -339,6 +339,10 @@ $(obj)u-boot.img: $(obj)u-boot.bin
sed -e 's/"[ ]*$$/ for $(BOARD) board"/') \
-d $< $@
+$(obj)u-boot.kwb: $(obj)u-boot.bin
+ $(obj)tools/mkimage -n $(KWD_CONFIG) -T kwbimage \
+ -a $(TEXT_BASE) -e $(TEXT_BASE) -d $< $@
+
$(obj)u-boot.sha1: $(obj)u-boot.bin
$(obj)tools/ubsha1 $(obj)u-boot.bin
@@ -3638,6 +3642,7 @@ clobber: clean
@rm -f $(OBJS) $(obj)*.bak $(obj)ctags $(obj)etags $(obj)TAGS \
$(obj)cscope.* $(obj)*.*~
@rm -f $(obj)u-boot $(obj)u-boot.map $(obj)u-boot.hex $(ALL)
+ @rm -f $(obj)u-boot.kwb
@rm -f $(obj)tools/{env/crc32.c,inca-swap-bytes}
@rm -f $(obj)cpu/mpc824x/bedbug_603e.c
@rm -f $(obj)include/asm/proc $(obj)include/asm/arch $(obj)include/asm
diff --git a/common/image.c b/common/image.c
index e22c974..845006f 100644
--- a/common/image.c
+++ b/common/image.c
@@ -145,6 +145,7 @@ static table_entry_t uimage_type[] = {
{ IH_TYPE_SCRIPT, "script", "Script", },
{ IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
{ IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
+ { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
{ -1, "", "", },
};
diff --git a/doc/README.kwbimage b/doc/README.kwbimage
new file mode 100644
index 0000000..b8a9b6b
--- /dev/null
+++ b/doc/README.kwbimage
@@ -0,0 +1,93 @@
+---------------------------------------------
+Kirkwood Boot Image generation using mkimage
+---------------------------------------------
+
+This document describes the U-Boot feature as it
+is implemented for the Kirkwood family of SoCs.
+
+The Kirkwood SoC's can boot directly from NAND FLASH,
+SPI FLASH, SATA etc. using its internal bootRom support.
+
+for more details refer section 24.2 of Kirkwood functional specifications.
+ref: www.marvell.com/products/embedded.../kirkwood/index.jsp
+
+Commad syntax:
+--------------
+./tools/mkimage -l <kwboot_file>
+ to list the kwb image file details
+
+./tools/mkimage -n <board specific configuration file> \
+ -T kwbimage -a <start address> -e <execution address> \
+ -d <input_raw_binary> <output_kwboot_file>
+
+for ex.
+./tools/mkimage -n ./board/Marvell/openrd_base/kwbimage.cfg \
+ -T kwbimage -a 0x00600000 -e 0x00600000 \
+ -d u-boot.bin u-boot.kwb
+
+kwimage support available with mkimage utility will generate kirkwood boot
+image that can be flashed on the board NAND/SPI flash
+
+Board specific configuration file specifications:
+------------------------------------------------
+1. This file must present in the $(BOARDDIR) and the name should be
+ kwbimage.cfg (since this is used in Makefile)
+2. This file can have empty lines and lines starting with "#" as first
+ character to put comments
+3. This file can have configuration command lines as mentioned below,
+ any other information in this file is treated as invalid.
+
+Configuration command line syntax:
+---------------------------------
+1. Each command line is must have two strings, first one command or address
+ and second one data string
+2. Following are the valid command strings and associated data strings:-
+ Command string data string
+ -------------- -----------
+ BOOT_FROM nand/spi/sata
+ NAND_ECC_MODE default/rs/hamming/disabled
+ NAND_PAGE_SIZE any uint16_t hex value
+ SATA_PIO_MODE any uint32_t hex value
+ DDR_INIT_DELAY any uint32_t hex value
+ <uint32_t reg addr> <uint32_t vlaue for it>,
+ you can have maximum 55 such register programming commands
+
+3. All commands are optional to program
+
+Typical example of kwimage.cfg file:
+-----------------------------------
+
+# Boot Media configurations
+BOOT_FROM nand
+NAND_ECC_MODE default
+NAND_PAGE_SIZE 0x0800
+
+# Configure RGMII-0 interface pad voltage to 1.8V
+0xFFD100e0 0x1b1b1b9b
+# DRAM Configuration
+0xFFD01400 0x43000c30
+0xFFD01404 0x37543000
+0xFFD01408 0x22125451
+0xFFD0140C 0x00000a33
+0xFFD01410 0x000000cc
+0xFFD01414 0x00000000
+0xFFD01418 0x00000000
+0xFFD0141C 0x00000C52
+0xFFD01420 0x00000040
+0xFFD01424 0x0000F17F
+0xFFD01428 0x00085520
+0xFFD0147C 0x00008552
+0xFFD01504 0x0FFFFFF1
+0xFFD01508 0x10000000
+0xFFD0150C 0x0FFFFFF5
+0xFFD01514 0x00000000
+0xFFD0151C 0x00000000
+0xFFD01494 0x00030000
+0xFFD01498 0x00000000
+0xFFD0149C 0x0000E803
+0xFFD01480 0x00000001
+# End of Header extension
+0x0 0x0
+
+------------------------------------------------
+Author: Prafulla Wadaskar <prafulla at marvell.com>
diff --git a/include/image.h b/include/image.h
index f183757..be322bd 100644
--- a/include/image.h
+++ b/include/image.h
@@ -158,6 +158,7 @@
#define IH_TYPE_SCRIPT 6 /* Script file */
#define IH_TYPE_FILESYSTEM 7 /* Filesystem Image (any type) */
#define IH_TYPE_FLATDT 8 /* Binary Flat Device Tree Blob */
+#define IH_TYPE_KWBIMAGE 9 /* Kirkwood Boot Image */
/*
* Compression Types
diff --git a/tools/Makefile b/tools/Makefile
index 43c284c..5ead74c 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -167,6 +167,7 @@ $(obj)img2srec$(SFX): $(obj)img2srec.o
$(STRIP) $@
$(obj)mkimage$(SFX): $(obj)mkimage.o $(obj)crc32.o $(obj)image.o $(obj)md5.o \
+ $(obj)kwbimage.o \
$(obj)sha1.o $(LIBFDT_OBJS) $(obj)os_support.o
$(CC) $(CFLAGS) $(HOST_LDFLAGS) -o $@ $^
$(STRIP) $@
diff --git a/tools/kwbimage.c b/tools/kwbimage.c
new file mode 100644
index 0000000..b1ad37a
--- /dev/null
+++ b/tools/kwbimage.c
@@ -0,0 +1,264 @@
+/*
+ * (C) Copyright 2008
+ * Marvell Semiconductor <www.marvell.com>
+ * Written-by: Prafulla Wadaskar <prafulla at marvell.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "kwbimage.h"
+
+static struct kwb_header kwbimage_header;
+static int datacmd_cnt = 0;
+
+/*
+ * Generates 8 bit checksum
+ */
+uint8_t kwbimage_checksum8(void *start, uint32_t len, uint8_t csum)
+{
+ register uint8_t sum = csum;
+ volatile uint8_t *startp = (volatile uint8_t *)start;
+
+ do {
+ sum += *startp;
+ startp++;
+ } while (--len);
+ return (sum);
+}
+
+/*
+ * Generates 32 bit checksum
+ */
+uint32_t kwbimage_checksum32(uint32_t *start, uint32_t len, uint32_t csum)
+{
+ register uint32_t sum = csum;
+ volatile uint32_t *startp = start;
+
+ do {
+ sum += *startp;
+ startp++;
+ len -= sizeof(uint32_t);
+ } while (len > 0);
+ return (sum);
+}
+
+void *kwbimage_get_header_ptr (void)
+{
+ return ((void *)&kwbimage_header);
+}
+
+int kwbimage_get_header_size (void)
+{
+ return (sizeof (struct kwb_header));
+}
+
+enum kwbimage_cmd kwbimage_check_cfgcmd(char *token,
+ struct kwb_header *kwbhdr)
+{
+ extbhr_t *exthdr = &kwbhdr->kwb_exthdr;
+ char tempptr[MAX_TEMPBUF_LEN];
+
+ /* command string processing */
+ if (strcmp(token, "BOOT_FROM") == 0)
+ return CMD_BOOT_FROM;
+ else if (strcmp(token, "NAND_ECC_MODE") == 0)
+ return CMD_NAND_ECC_MODE;
+ else if (strcmp(token, "NAND_PAGE_SIZE") == 0)
+ return CMD_NAND_PAGE_SIZE;
+ else if (strcmp(token, "SATA_PIO_MODE") == 0)
+ return CMD_SATA_PIO_MODE;
+ else if (strcmp(token, "DDR_INIT_DELAY") == 0)
+ return CMD_DDR_INIT_DELAY;
+ else {
+ exthdr->rcfg[datacmd_cnt].raddr =
+ (uint32_t) strtoul (token, tempptr, 16);
+ return CMD_DDR_DATA;
+ }
+}
+
+void kwbimage_check_cfgdata(char *token, enum kwbimage_cmd cmdsw,
+ struct kwb_header *kwbhdr)
+{
+ bhr_t *mhdr = &kwbhdr->kwb_hdr;
+ extbhr_t *exthdr = &kwbhdr->kwb_exthdr;
+ char tempptr[MAX_TEMPBUF_LEN];
+
+ switch(cmdsw) {
+ case CMD_BOOT_FROM:
+ if (strcmp(token, "spi") == 0) {
+ mhdr->blockid = IBR_HDR_SPI_ID;
+ } else if (strcmp(token, "nand") == 0) {
+ mhdr->blockid = IBR_HDR_NAND_ID;
+ } else if (strcmp(token, "sata") == 0) {
+ mhdr->blockid = IBR_HDR_SATA_ID;
+ } else {
+ printf("Err.. Invalid kwimage data\n");
+ exit (EXIT_FAILURE);
+ }
+ printf("Preparing kirkwood boot image to boot "
+ "from %s\n", token);
+ break;
+ case CMD_NAND_ECC_MODE:
+ if (strcmp(token, "default") == 0) {
+ mhdr->nandeccmode = IBR_HDR_ECC_DEFAULT;
+ } else if (strcmp(token, "hamming") == 0) {
+ mhdr->nandeccmode =
+ IBR_HDR_ECC_FORCED_HAMMING;
+ } else if (strcmp(token, "rs") == 0) {
+ mhdr->nandeccmode =
+ IBR_HDR_ECC_FORCED_RS;
+ } else if (strcmp(token, "disabled") == 0) {
+ mhdr->nandeccmode = IBR_HDR_ECC_DISABLED;
+ } else {
+ printf("Err.. Invalid kwimage data\n");
+ exit (EXIT_FAILURE);
+ }
+ printf("Nand ECC mode = %s\n", token);
+ break;
+ case CMD_NAND_PAGE_SIZE:
+ mhdr->nandpagesize =
+ (uint16_t) strtoul (token, tempptr, 16);
+ printf("Nand page size = %x\n", mhdr->nandpagesize);
+ break;
+ case CMD_SATA_PIO_MODE:
+ mhdr->satapiomode =
+ (uint8_t) strtoul (token, tempptr, 16);
+ printf("Sata PIO mode = %x\n", mhdr->satapiomode);
+ break;
+ case CMD_DDR_INIT_DELAY:
+ mhdr->ddrinitdelay =
+ (uint16_t) strtoul (token, tempptr, 16);
+ printf("DDR init delay = %d msec\n",
+ mhdr->ddrinitdelay);
+ break;
+ case CMD_DDR_DATA:
+ exthdr->rcfg[datacmd_cnt].rdata =
+ (uint32_t) strtoul (token, tempptr, 16);
+
+ if (datacmd_cnt > KWBIMAGE_MAX_CONFIG ) {
+ printf("Err.. found more than max "
+ "allowed(%d) configurations\n",
+ KWBIMAGE_MAX_CONFIG);
+ exit (EXIT_FAILURE);
+ } else
+ datacmd_cnt++;
+ break;
+ case CMD_INVALID:
+ default:
+ printf("Error.. invalid data %s\n",token);
+ exit (EXIT_FAILURE);
+ }
+}
+
+void kwdimage_set_ext_header(struct kwb_header *kwbhdr, char* fname) {
+ bhr_t *mhdr = &kwbhdr->kwb_hdr;
+ extbhr_t *exthdr = &kwbhdr->kwb_exthdr;
+ int fd = -1;
+ int lineno, j;
+ char *line = NULL;
+ char * token, *saveptr1, *saveptr2;
+ size_t len = 0;
+ enum kwbimage_cmd cmd;
+
+ /* set dram register offset */
+ exthdr->dramregsoffs = (uint32_t)&exthdr->rcfg - (uint32_t)mhdr;
+
+ if ((fd = fopen(fname, "r")) == 0) {
+ if (strlen(fname))
+ fprintf (stderr, "Err: Can't open %s: %s\n",
+ fname, strerror(errno));
+ else
+ fprintf (stderr, "Err: kwbimage.cfg not specified "
+ "%s\n"
+ "add -n <file>/<kwbimage.cfg> to the cmd\n",
+ strerror(errno));
+ exit (EXIT_FAILURE);
+ }
+
+ /* Simple kwimage.cfg file parser */
+ lineno=0;
+ while ((getline(&line, &len, fd)) != -1) {
+ lineno++;
+ token = strtok_r(line, "\r\n", &saveptr1);
+ /* drop all lines with zero tokens (= empty lines) */
+ if (token == NULL)
+ continue;
+
+ for (j = 0, cmd = CMD_INVALID, line = token; ; line = NULL) {
+ token = strtok_r(line, " ", &saveptr2);
+ if (token == NULL)
+ break;
+ /* Drop all text starting with '#' as comments */
+ if (token[0] == '#')
+ break;
+
+ /* Process rest as valid config command line */
+ if (j == 0) {
+ cmd = kwbimage_check_cfgcmd(token, kwbhdr);
+ if (cmd == CMD_INVALID)
+ break;
+ } else if (j == 1) {
+ kwbimage_check_cfgdata(token, cmd, kwbhdr);
+ } else {
+ /*
+ * Command format permits only two strings
+ * on a line, i.e. command and data
+ * if more than two are observed, then error
+ * will be reported
+ */
+ printf("Error.. Invalid command line(%d)\n",
+ lineno);
+ exit (EXIT_FAILURE);
+ }
+ j++;
+ }
+ }
+ if (line)
+ free(line);
+
+ fclose(fd);
+}
+
+void kwbimage_set_header (struct kwb_header *hdr, struct stat *sbuf,
+ uint32_t dadr, uint32_t eadr, char *fname)
+{
+ bhr_t *mhdr = &hdr->kwb_hdr;
+ extbhr_t *exthdr = &hdr->kwb_exthdr;
+
+ mhdr->blocksize = sbuf->st_size - kwbimage_get_header_size();
+ mhdr->srcaddr = kwbimage_get_header_size();
+ mhdr->destaddr= dadr;
+ mhdr->execaddr =eadr;
+ mhdr->ext = 0x1; /* header extension appended */
+
+ kwdimage_set_ext_header(hdr, fname);
+ /* calculate checksums */
+ mhdr->checkSum = kwbimage_checksum8((void *)mhdr, sizeof(bhr_t), 0);
+ exthdr->checkSum = kwbimage_checksum8((void *)exthdr,
+ sizeof(extbhr_t), 0);
+}
diff --git a/tools/kwbimage.h b/tools/kwbimage.h
new file mode 100644
index 0000000..98ab34f
--- /dev/null
+++ b/tools/kwbimage.h
@@ -0,0 +1,104 @@
+/*
+ * (C) Copyright 2008
+ * Marvell Semiconductor <www.marvell.com>
+ * iWritten-by: Prafulla Wadaskar <prafulla at marvell.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _KWBIMAGE_H_
+#define _KWBIMAGE_H_
+
+#include <stdint.h>
+
+#define KWBIMAGE_MAX_CONFIG ((0x1dc - 0x20)/sizeof(struct reg_config))
+#define MAX_TEMPBUF_LEN 32
+
+/* NAND ECC Mode */
+#define IBR_HDR_ECC_DEFAULT 0x00
+#define IBR_HDR_ECC_FORCED_HAMMING 0x01
+#define IBR_HDR_ECC_FORCED_RS 0x02
+#define IBR_HDR_ECC_DISABLED 0x03
+
+/* Boot Type - block ID */
+#define IBR_HDR_I2C_ID 0x4D
+#define IBR_HDR_SPI_ID 0x5A
+#define IBR_HDR_NAND_ID 0x8B
+#define IBR_HDR_SATA_ID 0x78
+#define IBR_HDR_PEX_ID 0x9C
+#define IBR_HDR_UART_ID 0x69
+#define IBR_DEF_ATTRIB 0x00
+
+enum kwbimage_cmd {
+ CMD_INVALID,
+ CMD_BOOT_FROM,
+ CMD_NAND_ECC_MODE,
+ CMD_NAND_PAGE_SIZE,
+ CMD_SATA_PIO_MODE,
+ CMD_DDR_INIT_DELAY,
+ CMD_DDR_DATA
+};
+
+/* typedefs */
+typedef struct bhr_t {
+ uint8_t blockid; /*0 */
+ uint8_t nandeccmode; /*1 */
+ uint16_t nandpagesize; /*2-3 */
+ uint32_t blocksize; /*4-7 */
+ uint32_t rsvd1; /*8-11 */
+ uint32_t srcaddr; /*12-15 */
+ uint32_t destaddr; /*16-19 */
+ uint32_t execaddr; /*20-23 */
+ uint8_t satapiomode; /*24 */
+ uint8_t rsvd3; /*25 */
+ uint16_t ddrinitdelay; /*26-27 */
+ uint16_t rsvd2; /*28-29 */
+ uint8_t ext; /*30 */
+ uint8_t checkSum; /*31 */
+} bhr_t, *pbhr_t;
+
+struct reg_config {
+ uint32_t raddr;
+ uint32_t rdata;
+};
+
+typedef struct extbhr_t {
+ uint32_t dramregsoffs;
+ uint8_t rsrvd1[0x20 - sizeof(uint32_t)];
+ struct reg_config rcfg[KWBIMAGE_MAX_CONFIG];
+ uint8_t rsrvd2[7];
+ uint8_t checkSum;
+} extbhr_t, *pextbhr_t;
+
+struct kwb_header {
+ bhr_t kwb_hdr;
+ extbhr_t kwb_exthdr;
+};
+
+/*
+ * functions
+ */
+uint8_t kwbimage_checksum8(void *start, uint32_t len, uint8_t csum);
+uint32_t kwbimage_checksum32(uint32_t *start, uint32_t len, uint32_t csum);
+void *kwbimage_get_header_ptr(void);
+int kwbimage_get_header_size (void);
+void kwbimage_set_header (struct kwb_header *hdr, struct stat *sbuf,
+ uint32_t dadr, uint32_t eadr, char *fname);
+
+#endif /* _KWBIMAGE_H_ */
diff --git a/tools/mkimage.c b/tools/mkimage.c
index 40363a9..ba71874 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -24,6 +24,7 @@
#include "mkimage.h"
#include <image.h>
+#include "kwbimage.h"
extern int errno;
@@ -164,6 +165,10 @@ NXTARG: ;
(lflag && (dflag || fflag)))
usage();
+ if (((argc != 1) && (opt_type == IH_TYPE_KWBIMAGE)) &&
+ (xflag || lflag))
+ usage();
+
if (!eflag) {
ep = addr;
/* If XIP, entry point must be after the U-Boot header */
@@ -251,7 +256,12 @@ NXTARG: ;
*
* write dummy header, to be fixed later
*/
- hdr_size = image_get_header_size ();
+ if (opt_type == IH_TYPE_KWBIMAGE) {
+ hdr = kwbimage_get_header_ptr();
+ hdr_size = kwbimage_get_header_size ();
+ } else
+ hdr_size = image_get_header_size ();
+
memset (hdr, 0, hdr_size);
if (write(ifd, hdr, hdr_size) != hdr_size) {
fprintf (stderr, "%s: Write error on %s: %s\n",
@@ -339,6 +349,19 @@ NXTARG: ;
hdr = (image_header_t *)ptr;
+ /* Build new header */
+ if (opt_type == IH_TYPE_KWBIMAGE) {
+ checksum = kwbimage_checksum32((uint32_t *)ptr, sbuf.st_size, 0);
+
+ if (write(ifd, &checksum, sizeof(uint32_t))
+ != sizeof(uint32_t)) {
+ fprintf (stderr, "%s: Checksum wr err on %s: %s\n",
+ cmdname, imagefile, strerror(errno));
+ exit (EXIT_FAILURE);
+ }
+ sbuf.st_size += sizeof(uint32_t);
+ kwbimage_set_header (hdr, &sbuf, addr, ep, name);
+ } else {
checksum = crc32 (0,
(const char *)(ptr + hdr_size),
sbuf.st_size - hdr_size);
@@ -361,6 +384,7 @@ NXTARG: ;
image_print_contents (hdr);
+ }
(void) munmap((void *)ptr, sbuf.st_size);
/* We're a bit of paranoid */
--
1.5.3.4
More information about the U-Boot
mailing list