^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Simple utility to make a single-image install kernel with initial ramdisk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) for Sparc tftpbooting without need to set up nfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) Pete Zaitcev <zaitcev@yahoo.com> endian fixes for cross-compiles, 2000.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) Copyright (C) 2011 Sam Ravnborg <sam@ravnborg.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^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 <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Note: run this on an a.out kernel (use elftoaout for it),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * as PROM looks for a.out image only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define AOUT_TEXT_OFFSET 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int is64bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* align to power-of-two size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int align(int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (is64bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return (n + 0x1fff) & ~0x1fff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return (n + 0xfff) & ~0xfff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* read two bytes as big endian */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static unsigned short ld2(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return (p[0] << 8) | p[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* save 4 bytes as big endian */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static void st4(char *p, unsigned int x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) p[0] = x >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) p[1] = x >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) p[2] = x >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) p[3] = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void die(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) perror(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* fs_img.gz is an image of initial ramdisk. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) fprintf(stderr, "Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) fprintf(stderr, "\tKernel image will be modified in place.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int start_line(const char *line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (strcmp(line + 10, " _start\n") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) else if (strcmp(line + 18, " _start\n") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int end_line(const char *line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (strcmp(line + 10, " _end\n") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) else if (strcmp (line + 18, " _end\n") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Find address for start and end in System.map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * The file looks like this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * f0004000 ... _start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * f0379f79 ... _end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * 1234567890123456
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * ^coloumn 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * There is support for 64 bit addresses too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Return 0 if either start or end is not found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int get_start_end(const char *filename, unsigned int *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) FILE *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) char buffer[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) map = fopen(filename, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) die(filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) while (fgets(buffer, 1024, map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (start_line(buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *start = strtoul(buffer, NULL, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) else if (end_line(buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) *end = strtoul(buffer, NULL, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fclose (map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (*start == 0 || *end == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define LOOKBACK (128 * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define BUFSIZE 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Find the HdrS entry from head_32/head_64.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * We check if it is at the beginning of the file (sparc64 case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * and if not we search for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * When we search do so in steps of 4 as HdrS is on a 4-byte aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * address (it is on same alignment as sparc instructions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * Return the offset to the HdrS entry (as off_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static off_t get_hdrs_offset(int kernelfd, const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) char buffer[BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) off_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (lseek(kernelfd, 0, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) die("lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) die(filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (buffer[40] == 'H' && buffer[41] == 'd' &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) buffer[42] == 'r' && buffer[43] == 'S') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Find the gokernel label */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Decode offset from branch instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) offset = ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Go back 512 bytes so we do not miss HdrS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) offset -= LOOKBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* skip a.out header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) offset += AOUT_TEXT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (lseek(kernelfd, offset, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) die("lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) die(filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) for (i = 0; i < LOOKBACK; i += 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (buffer[i + 0] == 'H' && buffer[i + 1] == 'd' &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) buffer[i + 2] == 'r' && buffer[i + 3] == 'S') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return offset + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) fprintf (stderr, "Couldn't find headers signature in %s\n", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int main(int argc,char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) char buffer[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned int i, start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) off_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct stat s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) int image, tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (argc != 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (strcmp(argv[1], "64") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) is64bit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (stat (argv[4], &s) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) die(argv[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (!get_start_end(argv[3], &start, &end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) fprintf(stderr, "Could not determine start and end from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) argv[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if ((image = open(argv[2], O_RDWR)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) die(argv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (read(image, buffer, 512) != 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) die(argv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (memcmp(buffer, aout_magic, 4) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) fprintf (stderr, "Not a.out. Don't blame me.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * We need to fill in values for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * sparc_ramdisk_image + sparc_ramdisk_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * To locate these symbols search for the "HdrS" text which appear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * in the image a little before the gokernel symbol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * See definition of these in init_32.S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) offset = get_hdrs_offset(image, argv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* skip HdrS + LINUX_VERSION_CODE + HdrS version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) offset += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (lseek(image, offset, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) die("lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * root_flags = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * root_dev = 1 (RAMDISK_MAJOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * ram_flags = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * sparc_ramdisk_image = "PAGE aligned address after _end")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * sparc_ramdisk_size = size of image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) st4(buffer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) st4(buffer + 4, 0x01000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) st4(buffer + 8, align(end + 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) st4(buffer + 12, s.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (write(image, buffer + 2, 14) != 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) die(argv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* For sparc64 update a_text and clear a_data + a_bss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (is64bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (lseek(image, 4, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) die("lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* a_text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) st4(buffer, align(end + 32 + 8191) - (start & ~0x3fffffUL) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) s.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* a_data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) st4(buffer + 4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* a_bss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) st4(buffer + 8, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (write(image, buffer, 12) != 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) die(argv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* seek page aligned boundary in the image file and add boot image */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (lseek(image, AOUT_TEXT_OFFSET - start + align(end + 32), 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) die("lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if ((tail = open(argv[4], O_RDONLY)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) die(argv[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) while ((i = read(tail, buffer, 1024)) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (write(image, buffer, i) != i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) die(argv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (close(image) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) die("close");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (close(tail) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) die("close");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }