0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <fcntl.h>
0014 #include <unistd.h>
0015 #include <stdlib.h>
0016 #include <stdio.h>
0017
0018
0019
0020
0021 #ifndef MAXPARTITIONS
0022 #define MAXPARTITIONS 8
0023 #endif
0024
0025 #ifndef u8
0026 #define u8 unsigned char
0027 #endif
0028
0029 #ifndef u16
0030 #define u16 unsigned short
0031 #endif
0032
0033 #ifndef u32
0034 #define u32 unsigned int
0035 #endif
0036
0037 struct disklabel {
0038 u32 d_magic;
0039 u16 d_type, d_subtype;
0040 u8 d_typename[16];
0041 u8 d_packname[16];
0042 u32 d_secsize;
0043 u32 d_nsectors;
0044 u32 d_ntracks;
0045 u32 d_ncylinders;
0046 u32 d_secpercyl;
0047 u32 d_secprtunit;
0048 u16 d_sparespertrack;
0049 u16 d_sparespercyl;
0050 u32 d_acylinders;
0051 u16 d_rpm, d_interleave, d_trackskew, d_cylskew;
0052 u32 d_headswitch, d_trkseek, d_flags;
0053 u32 d_drivedata[5];
0054 u32 d_spare[5];
0055 u32 d_magic2;
0056 u16 d_checksum;
0057 u16 d_npartitions;
0058 u32 d_bbsize, d_sbsize;
0059 struct d_partition {
0060 u32 p_size;
0061 u32 p_offset;
0062 u32 p_fsize;
0063 u8 p_fstype;
0064 u8 p_frag;
0065 u16 p_cpg;
0066 } d_partitions[MAXPARTITIONS];
0067 };
0068
0069
0070 typedef union __bootblock {
0071 struct {
0072 char __pad1[64];
0073 struct disklabel __label;
0074 } __u1;
0075 struct {
0076 unsigned long __pad2[63];
0077 unsigned long __checksum;
0078 } __u2;
0079 char bootblock_bytes[512];
0080 unsigned long bootblock_quadwords[64];
0081 } bootblock;
0082
0083 #define bootblock_label __u1.__label
0084 #define bootblock_checksum __u2.__checksum
0085
0086 int main(int argc, char ** argv)
0087 {
0088 bootblock bootblock_from_disk;
0089 bootblock bootloader_image;
0090 int dev, fd;
0091 int i;
0092 int nread;
0093
0094
0095 if(argc != 3) {
0096 fprintf(stderr, "Usage: %s device lxboot\n", argv[0]);
0097 exit(0);
0098 }
0099
0100
0101 dev = open(argv[1], O_RDWR);
0102 if(dev < 0) {
0103 perror(argv[1]);
0104 exit(0);
0105 }
0106
0107
0108 fd = open(argv[2], O_RDONLY);
0109 if(fd < 0) {
0110 perror(argv[2]);
0111 close(dev);
0112 exit(0);
0113 }
0114
0115
0116 nread = read(fd, &bootloader_image, sizeof(bootblock));
0117 if(nread != sizeof(bootblock)) {
0118 perror("lxboot read");
0119 fprintf(stderr, "expected %zd, got %d\n", sizeof(bootblock), nread);
0120 exit(0);
0121 }
0122
0123
0124 nread = read(dev, &bootblock_from_disk, sizeof(bootblock));
0125 if(nread != sizeof(bootblock)) {
0126 perror("bootblock read");
0127 fprintf(stderr, "expected %zd, got %d\n", sizeof(bootblock), nread);
0128 exit(0);
0129 }
0130
0131
0132 bootloader_image.bootblock_label = bootblock_from_disk.bootblock_label;
0133
0134
0135 bootloader_image.bootblock_checksum = 0;
0136 for(i = 0; i < 63; i++) {
0137 bootloader_image.bootblock_checksum +=
0138 bootloader_image.bootblock_quadwords[i];
0139 }
0140
0141
0142 lseek(dev, 0L, SEEK_SET);
0143 if(write(dev, &bootloader_image, sizeof(bootblock)) != sizeof(bootblock)) {
0144 perror("bootblock write");
0145 exit(0);
0146 }
0147
0148 close(fd);
0149 close(dev);
0150 exit(0);
0151 }
0152
0153