patch zlib check
[platform/upstream/cdrkit.git] / genisoimage / boot-hppa.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /*
14  * Program boot-hppa.c - Handle HPPA boot extensions to iso9660.
15  *
16  * Written by Steve McIntyre <steve@einval.com> June 2004.
17  *
18  * Heavily inspired by palo:
19  *
20  ****************************************************************************
21  * This file is subject to the terms and conditions of the GNU General Public
22  * License.  See the file "COPYING" in the main directory of this archive
23  * for more details.
24  *
25  * Copyright (C) Hewlett-Packard (Paul Bame) paul_bame@hp.com
26  *
27  ****************************************************************************
28  * Copyright 2004 Steve McIntyre
29  *
30  * This program is free software; you can redistribute it and/or modify
31  * it under the terms of the GNU General Public License as published by
32  * the Free Software Foundation; either version 2, or (at your option)
33  * any later version.
34  *
35  * This program is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  * GNU General Public License for more details.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
43  */
44
45 #include <mconfig.h>
46 #include "genisoimage.h"
47 #include <fctldefs.h>
48 #include <utypes.h>
49 #include <intcvt.h>
50 #include "match.h"
51 #include "diskmbr.h"
52 #include "bootinfo.h"
53 #include <schily.h>
54 #include "endianconv.h"
55
56 extern long long alpha_hppa_boot_sector[256];
57 extern int boot_sector_initialized;
58
59 int     add_boot_hppa_cmdline(char *cmdline);
60 int     add_boot_hppa_kernel_32(char *filename);
61 int     add_boot_hppa_kernel_64(char *filename);
62 int     add_boot_hppa_bootloader(char *filename);
63 int     add_boot_hppa_ramdisk(char *filename);
64
65 static  int     boot_hppa_write(FILE *outfile);
66
67 static  char   *hppa_cmdline = NULL;
68 static  char   *hppa_kernel_32 = NULL;
69 static  char   *hppa_kernel_64 = NULL;
70 static  char   *hppa_bootloader = NULL;
71 static  char   *hppa_ramdisk = NULL;
72
73 /* Simple function: store the filename to be used later when we need
74    to find the boot file */
75 extern int add_boot_hppa_cmdline(char *cmdline)
76 {
77     char *ptr = NULL;
78     hppa_cmdline = strdup(cmdline);
79     ptr = hppa_cmdline;
80     while (*ptr)
81     {
82         if (',' == *ptr)
83             *ptr = ' ';
84         ptr++;
85     }    
86     return 0;
87 }
88
89 /* Simple function: store the filename to be used later when we need
90    to find the boot file */
91 extern int add_boot_hppa_kernel_32(char *filename)
92 {
93     hppa_kernel_32 = filename;
94     return 0;
95 }
96
97 /* Simple function: store the filename to be used later when we need
98    to find the boot file */
99 extern int add_boot_hppa_kernel_64(char *filename)
100 {
101     hppa_kernel_64 = filename;
102     return 0;
103 }
104
105 /* Simple function: store the filename to be used later when we need
106    to find the boot file */
107 extern int add_boot_hppa_bootloader(char *filename)
108 {
109     hppa_bootloader = filename;
110     return 0;
111 }
112
113 /* Simple function: store the filename to be used later when we need
114    to find the boot file */
115 extern int add_boot_hppa_ramdisk(char *filename)
116 {
117     hppa_ramdisk = filename;
118     return 0;
119 }
120
121 static void exit_fatal(char *type, char *filename)
122 {
123 #ifdef  USE_LIBSCHILY
124     comerrno(EX_BAD, "Uh oh, I can't find the %s '%s'!\n",
125              type, filename);
126 #else
127     fprintf(stderr, "Uh oh, I can't find the %s '%s'!\n",
128              type, filename);
129     exit(1);
130 #endif
131 }
132
133 static int boot_hppa_write(FILE *outfile)
134 {
135     struct directory_entry      *boot_file;     /* Boot file we need to search for */
136     unsigned long length = 0;
137     unsigned long extent = 0;
138     unsigned char *boot_sector = (unsigned char *) alpha_hppa_boot_sector;
139     int i = 0;
140
141     if (!boot_sector_initialized) {
142         memset(alpha_hppa_boot_sector, 0, sizeof(alpha_hppa_boot_sector));
143         boot_sector_initialized = 1;
144     }
145
146     printf("Address is: %p\n",alpha_hppa_boot_sector);
147
148     boot_sector[0] = 0x80;  /* magic */
149     boot_sector[1] = 0x00;  /* magic */
150     boot_sector[2] = 'P';
151     boot_sector[3] = 'A';
152     boot_sector[4] = 'L';
153     boot_sector[5] = 'O';
154     boot_sector[6] = 0x00;
155     boot_sector[7] = 0x04;  /* version */
156
157     /* Find the dir entry for the 32-bit kernel by walking our file list */
158     boot_file = search_tree_file(root, hppa_kernel_32);
159     if (!boot_file)
160         exit_fatal("HPPA 32-bit kernel", hppa_kernel_32);
161     extent = 2048 * get_733(boot_file->isorec.extent);
162     length = get_733(boot_file->isorec.size);
163     fprintf(stderr, "Found hppa 32-bit kernel %s: using extent %lu (0x%lX), size %lu (0x%lX)\n",
164             hppa_kernel_32, extent, extent, length, length);
165     write_be32(extent, &boot_sector[8]);
166     write_be32(length, &boot_sector[12]);
167
168     /* Find the dir entry for the ramdisk by walking our file list */
169     boot_file = search_tree_file(root, hppa_ramdisk);
170     if (!boot_file)
171         exit_fatal("HPPA ramdisk", hppa_ramdisk);
172     extent = 2048 * get_733(boot_file->isorec.extent);
173     length = get_733(boot_file->isorec.size);
174     fprintf(stderr, "Found hppa ramdisk %s: using extent %lu (0x%lX), size %lu (0x%lX)\n",
175             hppa_ramdisk, extent, extent, length, length);
176     write_be32(extent, &boot_sector[16]);
177     write_be32(length, &boot_sector[20]);
178
179     /* Now the commandline */
180     snprintf((char *)&boot_sector[24], 127, "%s", hppa_cmdline);
181
182     /* Find the dir entry for the 64-bit kernel by walking our file list */
183     boot_file = search_tree_file(root, hppa_kernel_64);
184     if (!boot_file)
185         exit_fatal("HPPA 64-bit kernel", hppa_kernel_64);
186     extent = 2048 * get_733(boot_file->isorec.extent);
187     length = get_733(boot_file->isorec.size);
188     fprintf(stderr, "Found hppa 64-bit kernel %s: using extent %lu (0x%lX), size %lu (0x%lX)\n",
189             hppa_kernel_64, extent, extent, length, length);
190     write_be32(extent, &boot_sector[232]);
191     write_be32(length, &boot_sector[236]);
192
193     /* Find the dir entry for the IPL by walking our file list */
194     boot_file = search_tree_file(root, hppa_bootloader);
195     if (!boot_file)
196         exit_fatal("HPPA bootloader", hppa_bootloader);
197     extent = 2048 * get_733(boot_file->isorec.extent);
198     length = get_733(boot_file->isorec.size);
199     fprintf(stderr, "Found hppa bootloader %s: using extent %lu (0x%lX), size %lu (0x%lX)\n",
200             hppa_bootloader, extent, extent, length, length);
201     write_be32(extent, &boot_sector[240]);
202     write_be32(length, &boot_sector[244]);
203
204     return 0;
205 }
206
207 struct output_fragment hppaboot_desc = {NULL, NULL, NULL, boot_hppa_write, "hppa boot block"};