Commit c32527a1 authored by Al Viro's avatar Al Viro Committed by Paul Mackerras

[POWERPC] rewrite mkprep and mkbugboot in sane C

Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Signed-off-by: default avatarPaul Mackerras <paulus@samba.org>
parent 8c42ec2c
...@@ -19,36 +19,13 @@ ...@@ -19,36 +19,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <netinet/in.h>
#ifdef __sun__ #ifdef __sun__
#include <inttypes.h> #include <inttypes.h>
#else #else
#include <stdint.h> #include <stdint.h>
#endif #endif
#ifdef __i386__
#define cpu_to_be32(x) le32_to_cpu(x)
#define cpu_to_be16(x) le16_to_cpu(x)
#else
#define cpu_to_be32(x) (x)
#define cpu_to_be16(x) (x)
#endif
#define cpu_to_le32(x) le32_to_cpu((x))
unsigned long le32_to_cpu(unsigned long x)
{
return (((x & 0x000000ffU) << 24) |
((x & 0x0000ff00U) << 8) |
((x & 0x00ff0000U) >> 8) |
((x & 0xff000000U) >> 24));
}
#define cpu_to_le16(x) le16_to_cpu((x))
unsigned short le16_to_cpu(unsigned short x)
{
return (((x & 0x00ff) << 8) |
((x & 0xff00) >> 8));
}
/* size of read buffer */ /* size of read buffer */
#define SIZE 0x1000 #define SIZE 0x1000
...@@ -62,124 +39,109 @@ typedef struct bug_boot_header { ...@@ -62,124 +39,109 @@ typedef struct bug_boot_header {
#define HEADER_SIZE sizeof(bug_boot_header_t) #define HEADER_SIZE sizeof(bug_boot_header_t)
uint32_t copy_image(int32_t in_fd, int32_t out_fd) void update_checksum(void *buf, size_t size, uint16_t *sum)
{ {
uint8_t buf[SIZE]; uint32_t csum = *sum;
int n;
uint32_t image_size = 0; while (size) {
uint8_t zero = 0; csum += *(uint16_t *)buf;
if (csum > 0xffff)
lseek(in_fd, ELF_HEADER_SIZE, SEEK_SET); csum -= 0xffff;
buf = (uint16_t *)buf + 1;
/* Copy an image while recording its size */ size -= 2;
while ( (n = read(in_fd, buf, SIZE)) > 0 ) }
{ *sum = csum;
image_size = image_size + n;
write(out_fd, buf, n);
}
/* BUG romboot requires that our size is divisible by 2 */
/* align image to 2 byte boundary */
if (image_size % 2)
{
image_size++;
write(out_fd, &zero, 1);
}
return image_size;
} }
void write_bugboot_header(int32_t out_fd, uint32_t boot_size) uint32_t copy_image(int in_fd, int out_fd, uint16_t *sum)
{ {
uint8_t header_block[HEADER_SIZE]; uint8_t buf[SIZE];
bug_boot_header_t *bbh = (bug_boot_header_t *)&header_block[0]; int offset = 0;
int n;
memset(header_block, 0, HEADER_SIZE); uint32_t image_size = 0;
/* Fill in the PPCBUG ROM boot header */ lseek(in_fd, ELF_HEADER_SIZE, SEEK_SET);
strncpy(bbh->magic_word, "BOOT", 4); /* PPCBUG magic word */
bbh->entry_offset = cpu_to_be32(HEADER_SIZE); /* Entry address */ /* Copy an image while recording its size */
bbh->routine_length= cpu_to_be32(HEADER_SIZE+boot_size+2); /* Routine length */ while ( (n = read(in_fd, buf + offset, SIZE - offset)) > 0 ) {
strncpy(bbh->routine_name, "LINUXROM", 8); /* Routine name */ n += offset;
offset = n & 1;
/* Output the header and bootloader to the file */ n -= offset;
write(out_fd, header_block, HEADER_SIZE); image_size = image_size + n;
/* who's going to deal with short writes? */
write(out_fd, buf, n);
update_checksum(buf, n, sum);
if (offset)
buf[0] = buf[n];
}
/* BUG romboot requires that our size is divisible by 2 */
/* align image to 2 byte boundary */
if (offset) {
image_size += 2;
buf[1] = '\0';
write(out_fd, buf, 2);
update_checksum(buf, 2, sum);
}
return image_size;
} }
uint16_t calc_checksum(int32_t bug_fd) void write_bugboot_header(int out_fd, uint32_t boot_size, uint16_t *sum)
{ {
uint32_t checksum_var = 0; static bug_boot_header_t bbh = {
uint8_t buf[2]; .magic_word = "BOOT",
int n; .routine_name = "LINUXROM"
};
/* Checksum loop */
while ( (n = read(bug_fd, buf, 2) ) ) /* Fill in the PPCBUG ROM boot header */
{ bbh.entry_offset = htonl(HEADER_SIZE); /* Entry address */
checksum_var = checksum_var + *(uint16_t *)buf; bbh.routine_length= htonl(HEADER_SIZE+boot_size+2); /* Routine length */
/* If we carry out, mask it and add one to the checksum */ /* Output the header and bootloader to the file */
if (checksum_var >> 16) write(out_fd, &bbh, sizeof(bug_boot_header_t));
checksum_var = (checksum_var & 0x0000ffff) + 1; update_checksum(&bbh, sizeof(bug_boot_header_t), sum);
}
return checksum_var;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int32_t image_fd, bugboot_fd; int image_fd, bugboot_fd;
int argptr = 1; uint32_t kernel_size = 0;
uint32_t kernel_size = 0; uint16_t checksum = 0;
uint16_t checksum = 0;
uint8_t bugbootname[256];
if ( (argc != 3) )
{
fprintf(stderr, "usage: %s <kernel_image> <bugboot>\n",argv[0]);
exit(-1);
}
/* Get file args */
/* kernel image file */
if ((image_fd = open( argv[argptr] , 0)) < 0)
exit(-1);
argptr++;
/* bugboot file */ if (argc != 3) {
if ( !strcmp( argv[argptr], "-" ) ) fprintf(stderr, "usage: %s <kernel_image> <bugboot>\n",argv[0]);
bugboot_fd = 1; /* stdout */ exit(-1);
else }
if ((bugboot_fd = creat( argv[argptr] , 0755)) < 0)
exit(-1);
else
strcpy(bugbootname, argv[argptr]);
argptr++;
/* Set file position after ROM header block where zImage will be written */ /* Get file args */
lseek(bugboot_fd, HEADER_SIZE, SEEK_SET);
/* Copy kernel image into bugboot image */ /* kernel image file */
kernel_size = copy_image(image_fd, bugboot_fd); if ((image_fd = open(argv[1] , 0)) < 0)
close(image_fd); exit(-1);
/* Set file position to beginning where header/romboot will be written */ /* bugboot file */
lseek(bugboot_fd, 0, SEEK_SET); if (!strcmp(argv[2], "-"))
bugboot_fd = 1; /* stdout */
else if ((bugboot_fd = creat(argv[2] , 0755)) < 0)
exit(-1);
/* Write out BUG header/romboot */ /* Set file position after ROM header block where zImage will be written */
write_bugboot_header(bugboot_fd, kernel_size); lseek(bugboot_fd, HEADER_SIZE, SEEK_SET);
/* Close bugboot file */ /* Copy kernel image into bugboot image */
close(bugboot_fd); kernel_size = copy_image(image_fd, bugboot_fd, &checksum);
/* Reopen it as read/write */ /* Set file position to beginning where header/romboot will be written */
bugboot_fd = open(bugbootname, O_RDWR); lseek(bugboot_fd, 0, SEEK_SET);
/* Calculate checksum */ /* Write out BUG header/romboot */
checksum = calc_checksum(bugboot_fd); write_bugboot_header(bugboot_fd, kernel_size, &checksum);
/* Write out the calculated checksum */ /* Write out the calculated checksum */
write(bugboot_fd, &checksum, 2); lseek(bugboot_fd, 0, SEEK_END);
write(bugboot_fd, &checksum, 2);
return 0; /* Close bugboot file */
close(bugboot_fd);
return 0;
} }
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment