Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Makes a tree bootable image for IBM Evaluation boards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Basically, just take a zImage, skip the ELF header, and stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * a 32 byte header on the front.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * We use htonl, which is a network macro, to make sure we're doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * The Right Thing on an LE machine.  It's non-obvious, but it should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * work on anything BSD'ish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <netinet/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #ifdef __sun__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* This gets tacked on the front of the image.  There are also a few
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * bytes allocated after the _start label used by the boot rom (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * head.S for details).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) typedef struct boot_block {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	uint32_t bb_magic;		/* 0x0052504F */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	uint32_t bb_dest;		/* Target address of the image */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	uint32_t bb_num_512blocks;	/* Size, rounded-up, in 512 byte blks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	uint32_t bb_debug_flag;	/* Run debugger or image after load */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	uint32_t bb_entry_point;	/* The image address to start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	uint32_t bb_checksum;	/* 32 bit checksum including header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	uint32_t reserved[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) } boot_block_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define IMGBLK	512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) unsigned int	tmpbuf[IMGBLK / sizeof(unsigned int)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int	in_fd, out_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int	nblks, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	unsigned int	cksum, *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct	stat	st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	boot_block_t	bt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (argc < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		fprintf(stderr, "usage: %s <zImage-file> <boot-image> <load address> <entry point>\n",argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (stat(argv[1], &st) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		perror("stat");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	nblks = (st.st_size + IMGBLK) / IMGBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	bt.bb_magic = htonl(0x0052504F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* If we have the optional entry point parameter, use it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	bt.bb_dest = htonl(strtoul(argv[3], NULL, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	bt.bb_entry_point = htonl(strtoul(argv[4], NULL, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/* We know these from the linker command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * ...and then move it up into memory a little more so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 * relocation can happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	bt.bb_num_512blocks = htonl(nblks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	bt.bb_debug_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	bt.bb_checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	/* To be neat and tidy :-).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	bt.reserved[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	bt.reserved[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if ((in_fd = open(argv[1], O_RDONLY)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		perror("zImage open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		exit(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if ((out_fd = open(argv[2], (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		perror("bootfile open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		exit(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	cksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	cp = (void *)&bt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	for (i = 0; i < sizeof(bt) / sizeof(unsigned int); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		cksum += *cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* Assume zImage is an ELF file, and skip the 64K header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (read(in_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		fprintf(stderr, "%s is too small to be an ELF image\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 				argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		exit(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (tmpbuf[0] != htonl(0x7f454c46)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		fprintf(stderr, "%s is not an ELF image\n", argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		exit(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (lseek(in_fd, (64 * 1024), SEEK_SET) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		fprintf(stderr, "%s failed to seek in ELF image\n", argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		exit(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	nblks -= (64 * 1024) / IMGBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* And away we go......
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		perror("boot-image write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		exit(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	while (nblks-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		if (read(in_fd, tmpbuf, sizeof(tmpbuf)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			perror("zImage read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			exit(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		cp = tmpbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		for (i = 0; i < sizeof(tmpbuf) / sizeof(unsigned int); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			cksum += *cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (write(out_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			perror("boot-image write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			exit(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	/* rewrite the header with the computed checksum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	bt.bb_checksum = htonl(cksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (lseek(out_fd, 0, SEEK_SET) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		perror("rewrite seek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		perror("boot-image rewrite");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }