18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Makes a tree bootable image for IBM Evaluation boards.
48c2ecf20Sopenharmony_ci * Basically, just take a zImage, skip the ELF header, and stuff
58c2ecf20Sopenharmony_ci * a 32 byte header on the front.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * We use htonl, which is a network macro, to make sure we're doing
88c2ecf20Sopenharmony_ci * The Right Thing on an LE machine.  It's non-obvious, but it should
98c2ecf20Sopenharmony_ci * work on anything BSD'ish.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <fcntl.h>
138c2ecf20Sopenharmony_ci#include <stdio.h>
148c2ecf20Sopenharmony_ci#include <stdlib.h>
158c2ecf20Sopenharmony_ci#include <string.h>
168c2ecf20Sopenharmony_ci#include <sys/stat.h>
178c2ecf20Sopenharmony_ci#include <unistd.h>
188c2ecf20Sopenharmony_ci#include <netinet/in.h>
198c2ecf20Sopenharmony_ci#ifdef __sun__
208c2ecf20Sopenharmony_ci#include <inttypes.h>
218c2ecf20Sopenharmony_ci#else
228c2ecf20Sopenharmony_ci#include <stdint.h>
238c2ecf20Sopenharmony_ci#endif
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* This gets tacked on the front of the image.  There are also a few
268c2ecf20Sopenharmony_ci * bytes allocated after the _start label used by the boot rom (see
278c2ecf20Sopenharmony_ci * head.S for details).
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_citypedef struct boot_block {
308c2ecf20Sopenharmony_ci	uint32_t bb_magic;		/* 0x0052504F */
318c2ecf20Sopenharmony_ci	uint32_t bb_dest;		/* Target address of the image */
328c2ecf20Sopenharmony_ci	uint32_t bb_num_512blocks;	/* Size, rounded-up, in 512 byte blks */
338c2ecf20Sopenharmony_ci	uint32_t bb_debug_flag;	/* Run debugger or image after load */
348c2ecf20Sopenharmony_ci	uint32_t bb_entry_point;	/* The image address to start */
358c2ecf20Sopenharmony_ci	uint32_t bb_checksum;	/* 32 bit checksum including header */
368c2ecf20Sopenharmony_ci	uint32_t reserved[2];
378c2ecf20Sopenharmony_ci} boot_block_t;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define IMGBLK	512
408c2ecf20Sopenharmony_ciunsigned int	tmpbuf[IMGBLK / sizeof(unsigned int)];
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciint main(int argc, char *argv[])
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	int	in_fd, out_fd;
458c2ecf20Sopenharmony_ci	int	nblks, i;
468c2ecf20Sopenharmony_ci	unsigned int	cksum, *cp;
478c2ecf20Sopenharmony_ci	struct	stat	st;
488c2ecf20Sopenharmony_ci	boot_block_t	bt;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (argc < 5) {
518c2ecf20Sopenharmony_ci		fprintf(stderr, "usage: %s <zImage-file> <boot-image> <load address> <entry point>\n",argv[0]);
528c2ecf20Sopenharmony_ci		exit(1);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (stat(argv[1], &st) < 0) {
568c2ecf20Sopenharmony_ci		perror("stat");
578c2ecf20Sopenharmony_ci		exit(2);
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	nblks = (st.st_size + IMGBLK) / IMGBLK;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	bt.bb_magic = htonl(0x0052504F);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* If we have the optional entry point parameter, use it */
658c2ecf20Sopenharmony_ci	bt.bb_dest = htonl(strtoul(argv[3], NULL, 0));
668c2ecf20Sopenharmony_ci	bt.bb_entry_point = htonl(strtoul(argv[4], NULL, 0));
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	/* We know these from the linker command.
698c2ecf20Sopenharmony_ci	 * ...and then move it up into memory a little more so the
708c2ecf20Sopenharmony_ci	 * relocation can happen.
718c2ecf20Sopenharmony_ci	 */
728c2ecf20Sopenharmony_ci	bt.bb_num_512blocks = htonl(nblks);
738c2ecf20Sopenharmony_ci	bt.bb_debug_flag = 0;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	bt.bb_checksum = 0;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* To be neat and tidy :-).
788c2ecf20Sopenharmony_ci	*/
798c2ecf20Sopenharmony_ci	bt.reserved[0] = 0;
808c2ecf20Sopenharmony_ci	bt.reserved[1] = 0;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if ((in_fd = open(argv[1], O_RDONLY)) < 0) {
838c2ecf20Sopenharmony_ci		perror("zImage open");
848c2ecf20Sopenharmony_ci		exit(3);
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if ((out_fd = open(argv[2], (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
888c2ecf20Sopenharmony_ci		perror("bootfile open");
898c2ecf20Sopenharmony_ci		exit(3);
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	cksum = 0;
938c2ecf20Sopenharmony_ci	cp = (void *)&bt;
948c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(bt) / sizeof(unsigned int); i++)
958c2ecf20Sopenharmony_ci		cksum += *cp++;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* Assume zImage is an ELF file, and skip the 64K header.
988c2ecf20Sopenharmony_ci	*/
998c2ecf20Sopenharmony_ci	if (read(in_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
1008c2ecf20Sopenharmony_ci		fprintf(stderr, "%s is too small to be an ELF image\n",
1018c2ecf20Sopenharmony_ci				argv[1]);
1028c2ecf20Sopenharmony_ci		exit(4);
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (tmpbuf[0] != htonl(0x7f454c46)) {
1068c2ecf20Sopenharmony_ci		fprintf(stderr, "%s is not an ELF image\n", argv[1]);
1078c2ecf20Sopenharmony_ci		exit(4);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (lseek(in_fd, (64 * 1024), SEEK_SET) < 0) {
1118c2ecf20Sopenharmony_ci		fprintf(stderr, "%s failed to seek in ELF image\n", argv[1]);
1128c2ecf20Sopenharmony_ci		exit(4);
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	nblks -= (64 * 1024) / IMGBLK;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* And away we go......
1188c2ecf20Sopenharmony_ci	*/
1198c2ecf20Sopenharmony_ci	if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
1208c2ecf20Sopenharmony_ci		perror("boot-image write");
1218c2ecf20Sopenharmony_ci		exit(5);
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	while (nblks-- > 0) {
1258c2ecf20Sopenharmony_ci		if (read(in_fd, tmpbuf, sizeof(tmpbuf)) < 0) {
1268c2ecf20Sopenharmony_ci			perror("zImage read");
1278c2ecf20Sopenharmony_ci			exit(5);
1288c2ecf20Sopenharmony_ci		}
1298c2ecf20Sopenharmony_ci		cp = tmpbuf;
1308c2ecf20Sopenharmony_ci		for (i = 0; i < sizeof(tmpbuf) / sizeof(unsigned int); i++)
1318c2ecf20Sopenharmony_ci			cksum += *cp++;
1328c2ecf20Sopenharmony_ci		if (write(out_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
1338c2ecf20Sopenharmony_ci			perror("boot-image write");
1348c2ecf20Sopenharmony_ci			exit(5);
1358c2ecf20Sopenharmony_ci		}
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	/* rewrite the header with the computed checksum.
1398c2ecf20Sopenharmony_ci	*/
1408c2ecf20Sopenharmony_ci	bt.bb_checksum = htonl(cksum);
1418c2ecf20Sopenharmony_ci	if (lseek(out_fd, 0, SEEK_SET) < 0) {
1428c2ecf20Sopenharmony_ci		perror("rewrite seek");
1438c2ecf20Sopenharmony_ci		exit(1);
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
1468c2ecf20Sopenharmony_ci		perror("boot-image rewrite");
1478c2ecf20Sopenharmony_ci		exit(1);
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	exit(0);
1518c2ecf20Sopenharmony_ci}
152