aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/mkchkimg.c
blob: e152f7d4685b6b2a53133ea8b94ad8dd37657ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#
# Copyright (C) 2007-2015 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk

ARCH:=powerpc
BOARD:=ppc44x
BOARDNAME:=AMCC/IBM PPC44x
FEATURES:=squashfs broken
CPU_TYPE:=440
MAINTAINER:=Imre Kaloz <kaloz@openwrt.org>

KERNEL_PATCHVER:=3.18

define Target/Description
	Build firmware images for AMCC/IBM PPC44x based boards.
endef

include $(INCLUDE_DIR)/target.mk

KERNELNAME:=uImage cuImage.taishan

$(eval $(call BuildTarget))
6 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/*
 * 	Make CHK Image
 *
 * 	This utility creates Netgear .chk files.
 *
 * 	Copyright (C) 2008 Dave C. Reeve <Dave.Reeve@dreeve.org>
 *
 *	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.,
 *	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <arpa/inet.h>
#include <unistd.h>

#define BUF_LEN (2048)

#define MAX_BOARD_ID_LEN (64)

struct chk_header {
	uint32_t magic;
	uint32_t header_len;
	uint8_t  reserved[8];
	uint32_t kernel_chksum;
	uint32_t rootfs_chksum;
	uint32_t kernel_len;
	uint32_t rootfs_len;
	uint32_t image_chksum;
	uint32_t header_chksum;
	/* char board_id[] - upto MAX_BOARD_ID_LEN */
};

static void __attribute__ ((format (printf, 2, 3)))
fatal_error (int maybe_errno, const char * format, ...)
{
	va_list ap;

	fprintf (stderr, "mkchkimg: ");
	va_start (ap, format);
	vfprintf (stderr, format, ap);
	va_end (ap);

	if (maybe_errno) {
		fprintf (stderr, ": %s\n", strerror (maybe_errno));
	} else {
		fprintf (stderr, "\n");
	}

	exit (EXIT_FAILURE);
}

static void __attribute__ ((format (printf, 1, 2)))
message (const char * format, ...)
{
	va_list ap;

	fprintf (stderr, "mkchkimg: ");
	va_start (ap, format);
	vfprintf (stderr, format, ap);
	va_end (ap);
	fprintf (stderr, "\n");
}

struct ngr_checksum {
	uint32_t c0;
	uint32_t c1;
};

static inline void
netgear_checksum_init (struct ngr_checksum * c)
{
	c->c0 = c->c1 = 0;
}

static inline void
netgear_checksum_add (struct ngr_checksum * c, unsigned char * buf, size_t len)
{
	size_t i;

	for (i=0; i<len; i++) {
		c->c0 += buf[i] & 0xff;
		c->c1 += c->c0;
	}
}

static inline unsigned long
netgear_checksum_fini (struct ngr_checksum * c)
{
	uint32_t b, checksum;

	b = (c->c0 & 65535) + ((c->c0 >> 16) & 65535);
	c->c0 = ((b >> 16) + b) & 65535;
	b = (c->c1 & 65535) + ((c->c1 >> 16) & 65535);
	c->c1 = ((b >> 16) + b) & 65535;
	checksum = ((c->c1 << 16) | c->c0);
	return checksum;
}

static void
print_help (void)
{
	fprintf (stderr, "Usage: mkchkimg -o output -k kernel [-f filesys] [-b board_id] [-r region]\n");
}

int
main (int argc, char * argv[])
{
	int opt;
	char * ptr;
	size_t len;
	size_t header_len;
	struct chk_header * hdr;
	struct ngr_checksum chk_part, chk_whole;
	char buf[BUF_LEN];
	char * output_file, * kern_file, * fs_file;
	FILE * out_fp, * kern_fp, * fs_fp;
	char * board_id;
	unsigned long region;

	/* Default values */
	board_id = "U12H072T00_NETGEAR";
	region = 1;	/* 1=WW, 2=NA */
	output_file = NULL;
	kern_file = NULL;
	fs_file = NULL;
	fs_fp = NULL;

	while ((opt = getopt (argc, argv, ":b:r:k:f:o:h")) != -1) {
		switch (opt) {
		    case 'b':
		    	/* Board Identity */
			if (strlen (optarg) > MAX_BOARD_ID_LEN) {
				fatal_error (0, "Board lenght exceeds %d", 
					MAX_BOARD_ID_LEN);
			}
			board_id = optarg;
			break;

		    case 'r':
		    	/* Region */
			errno = 0;
			region = strtoul (optarg, &ptr, 0);
			if (errno || ptr==optarg || *ptr!='\0') {
				fatal_error (0, "Cannot parse region %s", optarg);
			}
			if (region > 0xff) {
				fatal_error (0, "Region cannot exceed 0xff");
			}
			break;

		    case 'k':
		    	/* Kernel */
			kern_file = optarg;
			break;

		    case 'f':
		    	/* Filing System */
			fs_file = optarg;
			break;

		    case 'o':
		    	/* Output file */
			output_file = optarg;
			break;

		    case 'h':
		    	print_help ();
			return EXIT_SUCCESS;

		    case ':':
		    	print_help ();
		    	fatal_error (0, "Option -%c missing argument", optopt);
			break;

		    case '?':
		    	print_help ();
		    	fatal_error (0, "Unknown argument -%c", optopt);
			break;
		    
		    default:
		    	break;
		}
	}

	/* Check we have all the options expected */
	if (!kern_file) {
		print_help ();
		fatal_error (0, "Kernel file expected");
	}
	if (!output_file) {
		print_help ();
		fatal_error (0, "Output file required");
	}
	message ("Netgear CHK writer - v0.1");

	/* Open the input file */
	kern_fp = fopen (kern_file, "r");
	if (!kern_fp) {
		fatal_error (errno, "Cannot open %s", kern_file);
	}

	/* Open the fs file, if specified */
	if (fs_file) {
		fs_fp = fopen (fs_file, "r");
		if (!fs_fp) {
			fatal_error (errno, "Cannot open %s", fs_file);
		}
	}

	/* Open the output file */
	out_fp = fopen (output_file, "w+");
	if (!out_fp) {
		fatal_error (errno, "Cannot open %s", output_file);
	}

	/* Write zeros when the chk header will be */
	buf[0] = '\0';
	header_len = sizeof (struct chk_header) + strlen (board_id);
	if (fwrite (buf, 1, header_len, out_fp) != header_len) {
		fatal_error (errno, "Cannot write header");
	}

	/* Allocate storage for header, we fill in as we go */
	hdr = malloc (sizeof (struct chk_header));
	if (!hdr) {
		fatal_error (0, "malloc failed");
	}
	bzero (hdr, sizeof (struct chk_header));

	/* Fill in known values */
	hdr->magic = htonl (0x2a23245e);
	hdr->header_len = htonl(header_len);
	hdr->reserved[0] = (unsigned char)(region & 0xff);
	hdr->reserved[1] = 1;		/* Major */
	hdr->reserved[2] = 1;		/* Minor */
	hdr->reserved[3] = 99;		/* Build */
	hdr->reserved[4] = 0;		/* Unknown t1 ? was 1 */
	hdr->reserved[5] = 0;		/* Unknonw t2 ? was 0 */
	hdr->reserved[6] = 0;		/* Unknonw t3 ? was 1 */
	hdr->reserved[7] = 0;		/* Unused ? */
	message ("       Board Id: %s", board_id);
	message ("         Region: %s", region == 1 ? "World Wide (WW)" 
			: (region == 2 ? "North America (NA)" : "Unknown"));

	/* Copy the trx file, calculating the checksum as we go */
	netgear_checksum_init (&chk_part);
	netgear_checksum_init (&chk_whole);
	while (!feof (kern_fp)) {
		len = fread (buf, 1, BUF_LEN, kern_fp);
		if (len < 1) {
			break;
		}
		if (fwrite (buf, len, 1, out_fp) != 1) {
			fatal_error (errno, "Write error");
		}
		hdr->kernel_len += len;
		netgear_checksum_add (&chk_part, (unsigned char *)buf, len);
		netgear_checksum_add (&chk_whole, (unsigned char *)buf, len);
	}
	hdr->kernel_chksum = netgear_checksum_fini (&chk_part);
	message ("     Kernel Len: %u", hdr->kernel_len);
	message ("Kernel Checksum: 0x%08x", hdr->kernel_chksum);
	hdr->kernel_len = htonl (hdr->kernel_len);
	hdr->kernel_chksum = htonl (hdr->kernel_chksum);

	/* Now copy the root fs, calculating the checksum as we go */
	if (fs_fp) {
		netgear_checksum_init (&chk_part);
		while (!feof (fs_fp)) {
			len = fread (buf, 1, BUF_LEN, fs_fp);
			if (len < 1) {
				break;
			}
			if (fwrite (buf, len, 1, out_fp) != 1) {
				fatal_error (errno, "Write error");
			}
			hdr->rootfs_len += len;
			netgear_checksum_add (&chk_part, (unsigned char *)buf, len);
			netgear_checksum_add (&chk_whole, (unsigned char *)buf, len);
		}
		hdr->rootfs_chksum = (netgear_checksum_fini (&chk_part));
		message ("     Rootfs Len: %u", hdr->rootfs_len);
		message ("Rootfs Checksum: 0x%08x", hdr->rootfs_chksum);
		hdr->rootfs_len = htonl (hdr->rootfs_len);
		hdr->rootfs_chksum = htonl (hdr->rootfs_chksum);
	}

	/* Calcautate the image checksum */
	hdr->image_chksum = netgear_checksum_fini (&chk_whole);
	message (" Image Checksum: 0x%08x", hdr->image_chksum);
	hdr->image_chksum = htonl (hdr->image_chksum);

	/* Calculate the header checksum */
	netgear_checksum_init (&chk_part);
	netgear_checksum_add (&chk_part, (unsigned char *)hdr, 
				sizeof (struct chk_header));
	netgear_checksum_add (&chk_part, (unsigned char *)board_id,
				strlen (board_id));
	hdr->header_chksum = htonl (netgear_checksum_fini (&chk_part));

	/* Finally rewind the output and write headers */
	rewind (out_fp);
	if (fwrite (hdr, sizeof (struct chk_header), 1, out_fp) != 1) {
		fatal_error (errno, "Cannot write header");
	}
	if (fwrite (board_id, strlen (board_id), 1, out_fp) != 1) {
		fatal_error (errno, "Cannot write board id");
	}

	/* Success */
	return EXIT_SUCCESS;
}