1 // SPDX-License-Identifier: GPL-2.0+
3 * Image manipulator for LPC32XX SoCs
5 * (C) Copyright 2015 DENX Software Engineering GmbH
6 * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
8 * Derived from omapimage.c:
11 * Linaro LTD, www.linaro.org
12 * Author: John Rigby <john.rigby@linaro.org>
13 * Based on TI's signGP.c
16 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
19 * Marvell Semiconductor <www.marvell.com>
20 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
23 #include "imagetool.h"
28 * NAND page 0 boot header
31 struct nand_page_0_boot_header {
37 * Default ICC (interface configuration data [sic]) if none specified
41 #ifndef LPC32XX_BOOT_ICR
42 #define LPC32XX_BOOT_ICR 0x00000096
46 * Default boot NAND page size if none specified in board config
49 #ifndef LPC32XX_BOOT_NAND_PAGESIZE
50 #define LPC32XX_BOOT_NAND_PAGESIZE 2048
54 * Default boot NAND pages per sector if none specified in board config
57 #ifndef LPC32XX_BOOT_NAND_PAGES_PER_SECTOR
58 #define LPC32XX_BOOT_NAND_PAGES_PER_SECTOR 64
62 * Maximum size for boot code is 56K unless defined in board config
65 #ifndef LPC32XX_BOOT_CODESIZE
66 #define LPC32XX_BOOT_CODESIZE (56*1024)
69 /* signature byte for a readable block */
71 #define LPC32XX_BOOT_BLOCK_OK 0xaa
73 static struct nand_page_0_boot_header lpc32xximage_header;
75 static int lpc32xximage_check_image_types(uint8_t type)
77 if (type == IH_TYPE_LPC32XXIMAGE)
82 static int lpc32xximage_verify_header(unsigned char *ptr, int image_size,
83 struct image_tool_params *params)
85 struct nand_page_0_boot_header *hdr =
86 (struct nand_page_0_boot_header *)ptr;
88 /* turn image size from bytes to NAND pages, page 0 included */
89 int image_size_in_pages = ((image_size - 1)
90 / LPC32XX_BOOT_NAND_PAGESIZE);
92 if (hdr->data[0] != (0xff & LPC32XX_BOOT_ICR))
94 if (hdr->data[1] != (0xff & ~LPC32XX_BOOT_ICR))
96 if (hdr->data[2] != (0xff & LPC32XX_BOOT_ICR))
98 if (hdr->data[3] != (0xff & ~LPC32XX_BOOT_ICR))
100 if (hdr->data[4] != (0xff & image_size_in_pages))
102 if (hdr->data[5] != (0xff & ~image_size_in_pages))
104 if (hdr->data[6] != (0xff & image_size_in_pages))
106 if (hdr->data[7] != (0xff & ~image_size_in_pages))
108 if (hdr->data[8] != (0xff & image_size_in_pages))
110 if (hdr->data[9] != (0xff & ~image_size_in_pages))
112 if (hdr->data[10] != (0xff & image_size_in_pages))
114 if (hdr->data[11] != (0xff & ~image_size_in_pages))
116 if (hdr->data[12] != LPC32XX_BOOT_BLOCK_OK)
118 if (hdr->data[128] != LPC32XX_BOOT_BLOCK_OK)
123 static void print_hdr_byte(struct nand_page_0_boot_header *hdr, int ofs)
125 printf("header[%d] = %02x\n", ofs, hdr->data[ofs]);
128 static void lpc32xximage_print_header(const void *ptr)
130 struct nand_page_0_boot_header *hdr =
131 (struct nand_page_0_boot_header *)ptr;
134 for (ofs = 0; ofs <= 12; ofs++)
135 print_hdr_byte(hdr, ofs);
136 print_hdr_byte(hdr, 128);
139 static void lpc32xximage_set_header(void *ptr, struct stat *sbuf, int ifd,
140 struct image_tool_params *params)
142 struct nand_page_0_boot_header *hdr =
143 (struct nand_page_0_boot_header *)ptr;
145 /* turn image size from bytes to NAND pages, page 0 included */
146 int image_size_in_pages = ((sbuf->st_size
147 + LPC32XX_BOOT_NAND_PAGESIZE - 1)
148 / LPC32XX_BOOT_NAND_PAGESIZE);
150 /* fill header -- default byte value is 0x00, not 0xFF */
151 memset((void *)hdr, 0, sizeof(*hdr));
152 hdr->data[0] = (hdr->data[2] = 0xff & LPC32XX_BOOT_ICR);
153 hdr->data[1] = (hdr->data[3] = 0xff & ~LPC32XX_BOOT_ICR);
154 hdr->data[4] = (hdr->data[6] = (hdr->data[8]
155 = (hdr->data[10] = 0xff & image_size_in_pages)));
156 hdr->data[5] = (hdr->data[7] = (hdr->data[9]
157 = (hdr->data[11] = 0xff & ~image_size_in_pages)));
158 hdr->data[12] = (hdr->data[128] = LPC32XX_BOOT_BLOCK_OK);
162 * lpc32xximage parameters
166 "LPC32XX Boot Image",
167 sizeof(lpc32xximage_header),
168 (void *)&lpc32xximage_header,
170 lpc32xximage_verify_header,
171 lpc32xximage_print_header,
172 lpc32xximage_set_header,
174 lpc32xximage_check_image_types,