blob: 1640c4199ca623294813f2dbee0d06d6b33590e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Makes a Motorola PPCBUG ROM bootable image which can be flashed
3 * into one of the FLASH banks on a Motorola PowerPlus board.
4 *
5 * Author: Matt Porter <mporter@mvista.com>
6 *
7 * 2001 (c) MontaVista, Software, Inc. This file is licensed under
8 * the terms of the GNU General Public License version 2. This program
9 * is licensed "as is" without any warranty of any kind, whether express
10 * or implied.
11 */
12
13#define ELF_HEADER_SIZE 65536
14
15#include <unistd.h>
16#include <sys/stat.h>
17#include <string.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <errno.h>
21#include <fcntl.h>
Al Viroc32527a2006-09-23 01:37:41 +010022#include <netinet/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#ifdef __sun__
24#include <inttypes.h>
25#else
26#include <stdint.h>
27#endif
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* size of read buffer */
30#define SIZE 0x1000
31
32/* PPCBUG ROM boot header */
33typedef struct bug_boot_header {
34 uint8_t magic_word[4]; /* "BOOT" */
35 uint32_t entry_offset; /* Offset from top of header to code */
36 uint32_t routine_length; /* Length of code */
37 uint8_t routine_name[8]; /* Name of the boot code */
38} bug_boot_header_t;
39
40#define HEADER_SIZE sizeof(bug_boot_header_t)
41
Al Viroc32527a2006-09-23 01:37:41 +010042void update_checksum(void *buf, size_t size, uint16_t *sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Al Viroc32527a2006-09-23 01:37:41 +010044 uint32_t csum = *sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Al Viroc32527a2006-09-23 01:37:41 +010046 while (size) {
47 csum += *(uint16_t *)buf;
48 if (csum > 0xffff)
49 csum -= 0xffff;
50 buf = (uint16_t *)buf + 1;
51 size -= 2;
52 }
53 *sum = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Al Viroc32527a2006-09-23 01:37:41 +010056uint32_t copy_image(int in_fd, int out_fd, uint16_t *sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Al Viroc32527a2006-09-23 01:37:41 +010058 uint8_t buf[SIZE];
59 int offset = 0;
60 int n;
61 uint32_t image_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Al Viroc32527a2006-09-23 01:37:41 +010063 lseek(in_fd, ELF_HEADER_SIZE, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Al Viroc32527a2006-09-23 01:37:41 +010065 /* Copy an image while recording its size */
66 while ( (n = read(in_fd, buf + offset, SIZE - offset)) > 0 ) {
67 n += offset;
68 offset = n & 1;
69 n -= offset;
70 image_size = image_size + n;
71 /* who's going to deal with short writes? */
72 write(out_fd, buf, n);
73 update_checksum(buf, n, sum);
74 if (offset)
75 buf[0] = buf[n];
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Al Viroc32527a2006-09-23 01:37:41 +010078 /* BUG romboot requires that our size is divisible by 2 */
79 /* align image to 2 byte boundary */
80 if (offset) {
81 image_size += 2;
82 buf[1] = '\0';
83 write(out_fd, buf, 2);
84 update_checksum(buf, 2, sum);
85 }
86 return image_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Al Viroc32527a2006-09-23 01:37:41 +010089void write_bugboot_header(int out_fd, uint32_t boot_size, uint16_t *sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Al Viroc32527a2006-09-23 01:37:41 +010091 static bug_boot_header_t bbh = {
92 .magic_word = "BOOT",
93 .routine_name = "LINUXROM"
94 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Al Viroc32527a2006-09-23 01:37:41 +010096 /* Fill in the PPCBUG ROM boot header */
97 bbh.entry_offset = htonl(HEADER_SIZE); /* Entry address */
98 bbh.routine_length= htonl(HEADER_SIZE+boot_size+2); /* Routine length */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Al Viroc32527a2006-09-23 01:37:41 +0100100 /* Output the header and bootloader to the file */
101 write(out_fd, &bbh, sizeof(bug_boot_header_t));
102 update_checksum(&bbh, sizeof(bug_boot_header_t), sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105int main(int argc, char *argv[])
106{
Al Viroc32527a2006-09-23 01:37:41 +0100107 int image_fd, bugboot_fd;
108 uint32_t kernel_size = 0;
109 uint16_t checksum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Al Viroc32527a2006-09-23 01:37:41 +0100111 if (argc != 3) {
112 fprintf(stderr, "usage: %s <kernel_image> <bugboot>\n",argv[0]);
113 exit(-1);
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Al Viroc32527a2006-09-23 01:37:41 +0100116 /* Get file args */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Al Viroc32527a2006-09-23 01:37:41 +0100118 /* kernel image file */
119 if ((image_fd = open(argv[1] , 0)) < 0)
120 exit(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Al Viroc32527a2006-09-23 01:37:41 +0100122 /* bugboot file */
123 if (!strcmp(argv[2], "-"))
124 bugboot_fd = 1; /* stdout */
125 else if ((bugboot_fd = creat(argv[2] , 0755)) < 0)
126 exit(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Al Viroc32527a2006-09-23 01:37:41 +0100128 /* Set file position after ROM header block where zImage will be written */
129 lseek(bugboot_fd, HEADER_SIZE, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Al Viroc32527a2006-09-23 01:37:41 +0100131 /* Copy kernel image into bugboot image */
132 kernel_size = copy_image(image_fd, bugboot_fd, &checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Al Viroc32527a2006-09-23 01:37:41 +0100134 /* Set file position to beginning where header/romboot will be written */
135 lseek(bugboot_fd, 0, SEEK_SET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Al Viroc32527a2006-09-23 01:37:41 +0100137 /* Write out BUG header/romboot */
138 write_bugboot_header(bugboot_fd, kernel_size, &checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Al Viroc32527a2006-09-23 01:37:41 +0100140 /* Write out the calculated checksum */
141 lseek(bugboot_fd, 0, SEEK_END);
142 write(bugboot_fd, &checksum, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Al Viroc32527a2006-09-23 01:37:41 +0100144 /* Close bugboot file */
145 close(bugboot_fd);
146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}