2
* s390-tools/zipl/src/install.c
3
* Functions handling the installation of the boot loader code onto disk.
5
* Copyright (C) 2001-2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7
* Author(s): Carsten Otte <cotte@de.ibm.com>
8
* Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
13
#include <sys/types.h>
15
#include <sys/ioctl.h>
33
/* Types of SCSI disk layouts */
42
/* Determine SCSI disk layout from the specified BOOTBLOCK. */
43
static enum scsi_layout
44
get_scsi_layout(unsigned char* bootblock)
46
if ((bootblock[510] == 0x55) && (bootblock[511] == 0xaa))
47
return scsi_layout_pcbios;
48
else if ((bootblock[508] == 0xda) && (bootblock[509] == 0xbe))
49
return scsi_layout_sun;
50
else if ((bootblock[0] == 0x0b) && (bootblock[1] == 0xe5) &&
51
(bootblock[2] == 0xa9) && (bootblock[3] == 0x41))
52
return scsi_layout_sgi;
53
return scsi_layout_unknown;
57
#define DISK_LAYOUT_ID 0x00000001
59
/* Create an IPL master boot record data structure for SCSI MBRs in memory
60
* at location BUFFER. TABLE contains a pointer to the program table. INFO
61
* provides information about the disk. */
63
update_scsi_mbr(void* bootblock, disk_blockptr_t* table,
64
struct disk_info* info)
70
uint8_t program_table_pointer[16];
74
switch (get_scsi_layout(bootblock)) {
75
case scsi_layout_pcbios:
77
printf("Detected SCSI PCBIOS disk layout.\n");
83
error_reason("Unsupported SCSI disk layout");
87
mbr = (struct scsi_mbr *) buffer;
88
memset(buffer, 0, sizeof(struct scsi_mbr));
89
memcpy(&mbr->magic, ZIPL_MAGIC, ZIPL_MAGIC_SIZE);
90
mbr->version_id = DISK_LAYOUT_ID;
91
bootmap_store_blockptr(&mbr->program_table_pointer, table, info);
96
/* Install bootloader for initial program load from a SCSI type disk. FD
97
* specifies the file descriptor of the device file. PROGRAM_TABLE points
98
* to the disk block containing the program table. INFO provides
99
* information about the disk type. Return 0 on success, non-zero otherwise. */
101
install_scsi(int fd, disk_blockptr_t* program_table, struct disk_info* info)
103
unsigned char* bootblock;
106
bootblock = (unsigned char*) misc_malloc(info->phy_block_size);
107
if (bootblock == NULL)
110
if (lseek(fd, 0, SEEK_SET) != 0) {
111
error_reason(strerror(errno));
112
error_text("Could not seek on device");
116
rc = misc_read(fd, bootblock, info->phy_block_size);
118
error_text("Could not read master boot record");
122
/* Put zIPL data into MBR */
123
rc = update_scsi_mbr(bootblock, program_table, info);
128
/* Write MBR back to disk */
130
printf("Writing SCSI master boot record.\n");
131
if (lseek(fd, 0, SEEK_SET) != 0) {
132
error_reason(strerror(errno));
133
error_text("Could not seek on device");
137
rc = misc_write(fd, bootblock, info->phy_block_size);
139
error_text("Could not write master boot record");
145
/* Install bootloader for initial program load from an FBA type disk. */
147
install_fba(int fd, disk_blockptr_t* program_table,
148
disk_blockptr_t* stage2_list, blocknum_t stage2_count,
149
struct disk_info* info)
151
struct boot_fba_stage0 stage0;
154
/* Initialize stage 0 data */
155
rc = boot_init_fba_stage0(&stage0, program_table, stage2_list,
158
/* Install stage 0 */
159
rc = misc_write(fd, &stage0, sizeof(struct boot_fba_stage0));
162
error_text("Could not write boot loader");
167
/* Install bootloader for initial program load from an ECKD type disk with
168
* Linux/390 classic disk layout. */
170
install_eckd_classic(int fd, disk_blockptr_t* program_table,
171
disk_blockptr_t* stage2_list, blocknum_t stage2_count,
172
struct disk_info* info, struct job_data* job)
174
struct boot_eckd_stage0 stage0;
175
struct boot_eckd_classic_stage1 stage1;
178
/* Prepare stage 0 */
179
boot_init_eckd_classic_stage0(&stage0);
180
/* Install stage 0 */
181
rc = misc_write(fd, &stage0, sizeof(struct boot_eckd_stage0));
183
error_text("Could not write boot loader");
186
/* Prepare stage 1 */
187
rc = boot_init_eckd_classic_stage1(&stage1, stage2_list, stage2_count,
191
/* Store program table pointer in stage 1 loader */
192
bootmap_store_blockptr(&stage1.param1, program_table, info);
193
/* Install stage 1 */
194
if (lseek(fd, info->phy_block_size, SEEK_SET) !=
195
info->phy_block_size) {
196
error_reason(strerror(errno));
197
error_text("Could not seek on device");
200
rc = misc_write(fd, &stage1, sizeof(struct boot_eckd_classic_stage1));
202
error_text("Could not write boot loader");
210
/* Install bootloader for initial program load from an ECKD type disk with
211
* OS/390 compatible disk layout. */
213
install_eckd_compatible(int fd, disk_blockptr_t* program_table,
214
struct disk_info* info, struct job_data* job)
216
struct boot_eckd_stage0 stage0;
217
struct boot_eckd_compatible_stage1 stage1;
222
/* Prepare stage 0 */
223
boot_init_eckd_compatible_stage0(&stage0);
224
/* Install stage 0 */
225
if (lseek(fd, 4, SEEK_SET) != 4) {
226
error_reason(strerror(errno));
227
error_text("Could not seek on device");
230
rc = misc_write(fd, &stage0, sizeof(struct boot_eckd_stage0));
232
error_text("Could not write boot loader");
235
/* Prepare stage 1 */
236
rc = boot_init_eckd_compatible_stage1(&stage1, info);
239
/* Store program table pointer in stage 1 loader */
240
bootmap_store_blockptr(&stage1.param1, program_table, info);
241
/* Install stage 1 */
242
if (lseek(fd, 4 + info->phy_block_size, SEEK_SET) !=
243
4 + info->phy_block_size) {
244
error_reason(strerror(errno));
245
error_text("Could not seek on device");
248
rc = misc_write(fd, &stage1,
249
sizeof(struct boot_eckd_compatible_stage1));
251
error_text("Could not write boot loader");
254
/* Install stage 2 */
255
if (lseek(fd, 3 * info->phy_block_size, SEEK_SET) !=
256
3 * info->phy_block_size) {
257
error_reason(strerror(errno));
258
error_text("Could not seek on device");
261
rc = boot_get_eckd_stage2(&buffer, &size, job);
264
rc = misc_write(fd, buffer, size);
266
error_text("Could not write boot loader");
273
install_bootloader(const char* device, disk_blockptr_t *program_table,
274
disk_blockptr_t* stage2_list, blocknum_t stage2_count,
275
struct disk_info* info, struct job_data* job)
280
/* Inform user about what we're up to */
281
printf("Preparing boot device: ");
283
printf("%s", info->name);
284
if (info->devno >= 0)
285
printf(" (%04x)", info->devno);
287
} else if (info->devno >= 0) {
288
printf("%04x.\n", info->devno);
290
disk_print_devt(info->device);
291
/* Open device file */
292
fd = open(device, O_RDWR);
294
error_reason(strerror(errno));
295
error_text("Could not open temporary device file '%s'",
299
/* Call disk specific install functions */
301
switch (info->type) {
303
rc = install_scsi(fd, program_table, info);
306
rc = install_fba(fd, program_table, stage2_list, stage2_count,
309
case disk_type_eckd_classic:
310
rc = install_eckd_classic(fd, program_table, stage2_list,
311
stage2_count, info, job);
313
case disk_type_eckd_compatible:
314
rc = install_eckd_compatible(fd, program_table, info, job);
317
case disk_type_unknown:
318
/* Should not happen */
323
if (info->devno >= 0)
324
syslog(LOG_INFO, "Boot loader written to %s (%04x) - "
326
(info->name ? info->name : "-"), info->devno,
327
major(info->device), minor(info->device));
329
syslog(LOG_INFO, "Boot loader written to %s - "
331
(info->name ? info->name : "-"),
332
major(info->device), minor(info->device));
338
/* Rewind the tape device identified by FD. Return 0 on success, non-zero
345
/* Magnetic tape rewind operation */
348
if (ioctl(fd, MTIOCTOP, &op) == -1)
356
ask_for_confirmation(const char* fmt, ...)
361
/* Always assume positive answer in non-interactive mode */
368
/* Process user reply */
369
scanf("%c", &answer);
370
if ((answer == 'y') || (answer == 'Y'))
372
error_text("Operation canceled by user");
377
/* Write data from file FILENAME to file descriptor FD. Data will be written
378
* in blocks of BLOCKSIZE bytes. Return 0 on success, non-zero otherwise. */
380
write_tapefile(int fd, const char* filename, size_t blocksize)
389
if (stat(filename, &stats)) {
390
error_reason(strerror(errno));
393
if (!S_ISREG(stats.st_mode)) {
394
error_reason("Not a regular file");
397
buffer = misc_malloc(blocksize);
400
read_fd = open(filename, O_RDONLY);
402
error_reason(strerror(errno));
406
for (offset = 0; offset < stats.st_size; offset += chunk) {
407
chunk = stats.st_size - offset;
408
if (chunk > blocksize)
411
memset(buffer, 0, blocksize);
412
if (misc_read(read_fd, buffer, chunk)) {
417
written = write(fd, buffer, chunk);
418
if (written != chunk) {
420
error_reason(strerror(errno));
422
error_reason("Write error");
434
/* Write SIZE bytes of data from memory location DATA to file descriptor FD.
435
* Data will be written in blocks of BLOCKSIZE bytes. Return 0 on success,
436
* non-zero otherwise. */
438
write_tapebuffer(int fd, const char* data, size_t size, size_t blocksize)
445
buffer = misc_malloc(blocksize);
448
for (offset = 0; offset < size; offset += chunk) {
449
chunk = size - offset;
450
if (chunk > blocksize)
453
memset(buffer, 0, blocksize);
454
memcpy(buffer, VOID_ADD(data, offset), chunk);
455
written = write(fd, buffer, chunk);
456
if (written != chunk) {
458
error_reason(strerror(errno));
460
error_reason("Write error");
470
/* Write COUNT tapemarks to file handle FD. */
472
write_tapemark(int fd, int count)
478
if (ioctl(fd, MTIOCTOP, &op) == -1) {
479
error_reason("Could not write tapemark");
486
#define IPL_TAPE_BLOCKSIZE 1024
488
/* Install IPL record on tape device. */
490
install_tapeloader(const char* device, const char* image, const char* parmline,
491
const char* ramdisk, address_t image_addr,
492
address_t parm_addr, address_t initrd_addr)
499
printf("Preparing boot tape: %s\n", device);
500
/* Prepare boot loader */
501
rc = boot_get_tape_ipl(&buffer, &size, parm_addr, initrd_addr,
506
fd = open(device, O_RDWR);
508
error_reason(strerror(errno));
509
error_text("Could not open tape device '%s'", device);
513
if (rewind_tape(fd) != 0) {
514
error_text("Could not rewind tape device '%s'", device);
519
/* Write boot loader */
520
rc = write_tapebuffer(fd, buffer, size, IPL_TAPE_BLOCKSIZE);
523
error_text("Could not write boot loader to tape");
527
rc = write_tapemark(fd, 1);
529
error_text("Could not write boot loader to tape");
533
/* Write image file */
535
printf(" kernel image......: %s at 0x%llx\n", image,
536
(unsigned long long) image_addr);
538
rc = write_tapefile(fd, image, IPL_TAPE_BLOCKSIZE);
540
error_text("Could not write image file '%s' to tape", image);
544
rc = write_tapemark(fd, 1);
546
error_text("Could not write boot loader to tape");
550
if (parmline != NULL) {
552
printf(" kernel parmline...: '%s' at 0x%llx\n",
553
parmline, (unsigned long long) parm_addr);
555
/* Write parameter line */
556
rc = write_tapebuffer(fd, parmline, strlen(parmline),
559
error_text("Could not write parameter string to tape");
564
rc = write_tapemark(fd, 1);
566
error_text("Could not write boot loader to tape");
570
if (ramdisk != NULL) {
573
printf(" initial ramdisk...: %s at 0x%llx\n",
574
ramdisk, (unsigned long long) initrd_addr);
576
rc = write_tapefile(fd, ramdisk, IPL_TAPE_BLOCKSIZE);
578
error_text("Could not write ramdisk file '%s' to tape",
584
rc = write_tapemark(fd, 1);
586
error_text("Could not write boot loader to tape");
590
if (rewind_tape(fd) != 0) {
591
error_text("Could not rewind tape device '%s' to tape", device);
599
struct eckd_dump_param {
605
} __attribute((packed));
608
install_dump_eckd_classic(int fd, struct disk_info* info, uint64_t mem)
610
struct boot_eckd_stage0 stage0;
611
struct boot_eckd_classic_stage1 stage1;
612
struct eckd_dump_param* param;
617
/* Prepare stage 0 */
618
boot_init_eckd_classic_stage0(&stage0);
619
/* Install stage 0 */
620
rc = misc_write(fd, &stage0, sizeof(struct boot_eckd_stage0));
622
error_text("Could not write boot loader");
625
/* Prepare stage 1 */
626
rc = boot_init_eckd_classic_dump_stage1(&stage1, info);
629
/* Fill in start of dump partition */
630
param = (struct eckd_dump_param *) &stage1.param1;
631
param->cyl = disk_cyl_from_blocknum(info->geo.start, info);
632
param->head = disk_head_from_blocknum(info->geo.start, info);
633
param->sec = disk_sec_from_blocknum(info->geo.start, info);
634
param->blocksize = info->phy_block_size;
635
param->num_heads = info->geo.heads;
636
/* Fill in end of dump partition. Note that on ECKD devices with
637
* classic disk layout, there is exactly one partition so that the
638
* end of the device equals the end of the partition. */
639
param = (struct eckd_dump_param *) &stage1.param2;
640
param->cyl = info->geo.cylinders - 1;
641
param->head = info->geo.heads - 1;
642
param->sec = info->geo.sectors;
643
param->blocksize = info->phy_block_size;
644
param->num_heads = 0; /* Unused */
645
/* Install stage 1 */
646
if (lseek(fd, info->phy_block_size, SEEK_SET) !=
647
info->phy_block_size) {
648
error_reason(strerror(errno));
649
error_text("Could not seek on device");
652
rc = misc_write(fd, &stage1, sizeof(struct boot_eckd_classic_stage1));
654
error_text("Could not write dump record");
657
/* Install stage 2 dump record at beginning of partition */
658
if (lseek(fd, info->geo.start * info->phy_block_size, SEEK_SET) !=
659
(off_t) info->geo.start * info->phy_block_size) {
660
error_text("Could not seek on device");
663
rc = boot_get_eckd_dump_stage2(&buffer, &size, mem);
666
rc = misc_write(fd, buffer, size);
668
error_text("Could not write dump record");
675
install_dump_eckd_compatible(int fd, struct disk_info* info, uint64_t mem)
677
struct boot_eckd_stage0 stage0;
678
struct boot_eckd_compatible_stage1 stage1;
679
struct eckd_dump_param* param;
684
/* Prepare stage 0 */
685
boot_init_eckd_compatible_stage0(&stage0);
686
/* Install stage 0 */
687
if (lseek(fd, 4, SEEK_SET) != 4) {
688
error_reason(strerror(errno));
689
error_text("Could not seek on device");
692
rc = misc_write(fd, &stage0, sizeof(struct boot_eckd_stage0));
694
error_text("Could not write boot loader");
697
/* Prepare stage 1 */
698
rc = boot_init_eckd_compatible_dump_stage1(&stage1, info);
701
/* Fill in start of dump partition */
702
param = (struct eckd_dump_param *) &stage1.param1;
703
param->cyl = disk_cyl_from_blocknum(info->geo.start, info);
704
param->head = disk_head_from_blocknum(info->geo.start, info);
705
param->sec = disk_sec_from_blocknum(info->geo.start, info);
706
param->blocksize = info->phy_block_size;
707
param->num_heads = info->geo.heads;
708
/* Fill in end of dump partition. */
709
param = (struct eckd_dump_param *) &stage1.param2;
710
param->cyl = disk_cyl_from_blocknum(info->geo.start +
711
info->phy_blocks - 1, info);
712
param->head = disk_head_from_blocknum(info->geo.start +
713
info->phy_blocks - 1, info);
714
param->sec = disk_sec_from_blocknum(info->geo.start +
715
info->phy_blocks - 1, info);
716
param->blocksize = info->phy_block_size;
717
param->num_heads = 0; /* Unused */
718
/* Install stage 1 */
719
if (lseek(fd, info->phy_block_size + 4, SEEK_SET) !=
720
info->phy_block_size + 4) {
721
error_reason(strerror(errno));
722
error_text("Could not seek on device");
725
rc = misc_write(fd, &stage1,
726
sizeof(struct boot_eckd_compatible_stage1));
728
error_text("Could not write dump record");
731
/* Install stage 2 dump record */
732
if (lseek(fd, 3 * info->phy_block_size, SEEK_SET) !=
733
3 * info->phy_block_size) {
734
error_text("Could not seek on device");
737
rc = boot_get_eckd_dump_stage2(&buffer, &size, mem);
740
rc = misc_write(fd, buffer, size);
742
error_text("Could not write dump record");
749
install_dump_tape(int fd, uint64_t mem)
755
rc = boot_get_tape_dump(&buffer, &size, mem);
758
rc = misc_write(fd, buffer, size);
760
error_text("Could not write to tape device");
767
install_dump(const char* device, uint64_t mem)
769
struct disk_info* info;
774
fd = open(device, O_RDWR);
776
error_reason(strerror(errno));
777
error_text("Could not open dump device '%s'", device);
780
if (rewind_tape(fd) == 0) {
781
/* Rewind worked - this is a tape */
782
rc = ask_for_confirmation("Warning: All information on device "
783
"'%s' will be lost!\nDo you want to "
784
"continue creating a one-shot dump "
785
"tape (y/n) ?", device);
791
printf("Installing tape dump record\n");
792
rc = install_dump_tape(fd, mem);
794
error_text("Could not install dump record on tape "
795
"device '%s'", device);
798
printf("Dump record successfully installed on "
799
"tape device '%s'.\n", device);
806
/* This is a disk device */
807
rc = disk_get_info(device, &info);
809
error_text("Could not get information for dump target "
814
if (info->partnum == 0) {
815
error_reason("Dump target '%s' is not a disk partition",
817
disk_free_info(info);
821
printf("Target device information\n");
822
disk_print_info(info);
824
rc = misc_temp_dev(info->device, 1, &tempdev);
826
disk_free_info(info);
829
fd = open(tempdev, O_RDWR);
831
error_text("Could not open temporary device node '%s'",
833
misc_free_temp_dev(tempdev);
834
disk_free_info(info);
837
switch (info->type) {
838
case disk_type_eckd_classic:
839
case disk_type_eckd_compatible:
840
rc = ask_for_confirmation("Warning: All information on "
841
"partition '%s' will be lost!\n"
842
"Do you want to continue creating "
843
"a dump partition (y/n)?", device);
847
printf("Installing dump record on partition with %s\n",
848
disk_get_type_name(info->type));
850
if (info->type == disk_type_eckd_classic)
851
rc = install_dump_eckd_classic(fd, info, mem);
853
rc = install_dump_eckd_compatible(fd, info, mem);
858
case disk_type_unknown:
859
error_reason("Unsupported disk type '%s' of dump target '%s'",
860
disk_get_type_name(info->type), device);
864
misc_free_temp_dev(tempdev);
865
disk_free_info(info);