1 /*
2 * arch/ppc/pp3boot/mkbugboot.c
3 *
4 * Makes a Motorola PPCBUG ROM bootable image which can be flashed
5 * into one of the FLASH banks on a Motorola PowerPlus board.
6 *
7 * Author: Matt Porter <mporter@mvista.com>
8 *
9 * Copyright 2001 MontaVista Software Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17 #define ELF_HEADER_SIZE 65536
18
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <fcntl.h>
26
27 #ifdef __i386__
28 #define cpu_to_be32(x) le32_to_cpu(x)
29 #define cpu_to_be16(x) le16_to_cpu(x)
30 #else
31 #define cpu_to_be32(x) (x)
32 #define cpu_to_be16(x) (x)
33 #endif
34
35 #define cpu_to_le32(x) le32_to_cpu((x))
le32_to_cpu(unsigned long x)36 unsigned long le32_to_cpu(unsigned long x)
37 {
38 return (((x & 0x000000ffU) << 24) |
39 ((x & 0x0000ff00U) << 8) |
40 ((x & 0x00ff0000U) >> 8) |
41 ((x & 0xff000000U) >> 24));
42 }
43
44 #define cpu_to_le16(x) le16_to_cpu((x))
le16_to_cpu(unsigned short x)45 unsigned short le16_to_cpu(unsigned short x)
46 {
47 return (((x & 0x00ff) << 8) |
48 ((x & 0xff00) >> 8));
49 }
50
51 /* size of read buffer */
52 #define SIZE 0x1000
53
54 /* PPCBUG ROM boot header */
55 typedef struct bug_boot_header {
56 unsigned char magic_word[4]; /* "BOOT" */
57 unsigned long entry_offset; /* Offset from top of header to code */
58 unsigned long routine_length; /* Length of code */
59 unsigned char routine_name[8]; /* Name of the boot code */
60 } bug_boot_header_t;
61
62 #define HEADER_SIZE sizeof(bug_boot_header_t)
63
copy_image(int32_t in_fd,int32_t out_fd)64 unsigned long copy_image(int32_t in_fd, int32_t out_fd)
65 {
66 unsigned char buf[SIZE];
67 int n;
68 unsigned long image_size = 0;
69 unsigned char zero = 0;
70
71 lseek(in_fd, ELF_HEADER_SIZE, SEEK_SET);
72
73 /* Copy an image while recording its size */
74 while ( (n = read(in_fd, buf, SIZE)) > 0 )
75 {
76 image_size = image_size + n;
77 write(out_fd, buf, n);
78 }
79
80 /* BUG romboot requires that our size is divisible by 2 */
81 /* align image to 2 byte boundary */
82 if (image_size % 2)
83 {
84 image_size++;
85 write(out_fd, &zero, 1);
86 }
87
88 return image_size;
89 }
90
write_bugboot_header(int32_t out_fd,unsigned long boot_size)91 void write_bugboot_header(int32_t out_fd, unsigned long boot_size)
92 {
93 unsigned char header_block[HEADER_SIZE];
94 bug_boot_header_t *bbh = (bug_boot_header_t *)&header_block[0];
95
96 memset(header_block, 0, HEADER_SIZE);
97
98 /* Fill in the PPCBUG ROM boot header */
99 strncpy(bbh->magic_word, "BOOT", 4); /* PPCBUG magic word */
100 bbh->entry_offset = cpu_to_be32(HEADER_SIZE); /* Entry address */
101 bbh->routine_length= cpu_to_be32(HEADER_SIZE+boot_size+2); /* Routine length */
102 strncpy(bbh->routine_name, "LINUXROM", 8); /* Routine name */
103
104 /* Output the header and bootloader to the file */
105 write(out_fd, header_block, HEADER_SIZE);
106 }
107
calc_checksum(int32_t bug_fd)108 unsigned short calc_checksum(int32_t bug_fd)
109 {
110 unsigned long checksum_var = 0;
111 unsigned char buf[2];
112 int n;
113
114 /* Checksum loop */
115 while ( (n = read(bug_fd, buf, 2) ) )
116 {
117 checksum_var = checksum_var + *(unsigned short *)buf;
118
119 /* If we carry out, mask it and add one to the checksum */
120 if (checksum_var >> 16)
121 checksum_var = (checksum_var & 0x0000ffff) + 1;
122 }
123
124 return checksum_var;
125 }
126
main(int argc,char * argv[])127 int main(int argc, char *argv[])
128 {
129 int32_t image_fd, bugboot_fd;
130 int argptr = 1;
131 unsigned long kernel_size = 0;
132 unsigned short checksum = 0;
133 unsigned char bugbootname[256];
134
135 if ( (argc != 3) )
136 {
137 fprintf(stderr, "usage: %s <kernel_image> <bugboot>\n",argv[0]);
138 exit(-1);
139 }
140
141 /* Get file args */
142
143 /* kernel image file */
144 if ((image_fd = open( argv[argptr] , 0)) < 0)
145 exit(-1);
146 argptr++;
147
148 /* bugboot file */
149 if ( !strcmp( argv[argptr], "-" ) )
150 bugboot_fd = 1; /* stdout */
151 else
152 if ((bugboot_fd = creat( argv[argptr] , 0755)) < 0)
153 exit(-1);
154 else
155 strcpy(bugbootname, argv[argptr]);
156 argptr++;
157
158 /* Set file position after ROM header block where zImage will be written */
159 lseek(bugboot_fd, HEADER_SIZE, SEEK_SET);
160
161 /* Copy kernel image into bugboot image */
162 kernel_size = copy_image(image_fd, bugboot_fd);
163 close(image_fd);
164
165 /* Set file position to beginning where header/romboot will be written */
166 lseek(bugboot_fd, 0, SEEK_SET);
167
168 /* Write out BUG header/romboot */
169 write_bugboot_header(bugboot_fd, kernel_size);
170
171 /* Close bugboot file */
172 close(bugboot_fd);
173
174 /* Reopen it as read/write */
175 bugboot_fd = open(bugbootname, O_RDWR);
176
177 /* Calculate checksum */
178 checksum = calc_checksum(bugboot_fd);
179
180 /* Write out the calculated checksum */
181 write(bugboot_fd, &checksum, 2);
182
183 return 0;
184 }
185