Convert build system to CMake
[hvf.git] / installer / cpio.c
blobf18cad55228ccb1cb7fb58f0234dc03311615275
1 /*
2 * Copyright (c) 2011 Josef 'Jeff' Sipek
3 */
4 #include "loader.h"
5 #include <string.h>
6 #include <ebcdic.h>
8 struct cpio_hdr {
9 u8 magic[6];
10 u8 dev[6];
11 u8 ino[6];
12 u8 mode[6];
13 u8 uid[6];
14 u8 gid[6];
15 u8 nlink[6];
16 u8 rdev[6];
17 u8 mtime[11];
18 u8 namesize[6];
19 u8 filesize[11];
20 u8 data[0];
23 struct table {
24 char *arch;
25 char fn[8];
26 char ft[8];
27 int lrecl;
28 int text;
29 u32 lba; /* 0 means undef */
32 static struct table table[] = {
33 {"hvf.directory", "HVF ", "DIRECT ", 80, 1, 0},
34 {"system.config", "SYSTEM ", "CONFIG ", 80, 1, 0},
35 {"local-3215.txt", "HVF ", "LOGO ", 80, 1, 0},
36 {"hvf", "HVF ", "ELF ", 4096, 0, 0},
37 {"eckd.rto", "ECKDLOAD", "BIN ", 4096, 0, 1},
38 {"loader.rto", "DASDLOAD", "BIN ", 4096, 0, 2},
39 {"installed_files.txt", "HVF ", "TEXT ", 80, 1, 0},
40 {"8ball", "8BALL ", "NSS ", 4096, 0, 0},
41 {"", "" , "" , -1, -1, 0},
44 static void save_file(struct table *te, int filesize, u8 *buf)
46 char pbuf[100];
47 struct FST fst;
48 int ret;
49 int rec;
51 ret = find_file(te->fn, te->ft, &fst);
52 if (!ret) {
53 wto("File '");
54 wto(te->fn);
55 wto("' already exists on the device.\n");
56 wto("The device has been left unmodified.\n");
57 die();
60 ret = create_file(te->fn, te->ft, te->lrecl, &fst);
61 if (ret) {
62 wto("Could not create file '");
63 wto(te->fn);
64 wto("'.\n");
65 die();
68 if (te->text)
69 ascii2ebcdic(buf, filesize);
71 if (te->lba) {
72 if (filesize > te->lrecl)
73 die();
74 snprintf(pbuf, 100, "special file, writing copy of data to LBA %d\n",
75 te->lba);
76 wto(pbuf);
77 write_blk(buf, te->lba);
80 for(rec=0; rec<(filesize/te->lrecl); rec++) {
81 snprintf(pbuf, 100, "rec %03x at %p\n", rec,
82 buf + (rec * te->lrecl));
83 wto(pbuf);
84 append_record(&fst, buf + (rec * te->lrecl));
87 if (filesize % te->lrecl) {
88 u8 buf2[te->lrecl];
90 memset(buf2, 0, te->lrecl);
91 memcpy(buf2, buf + (rec * te->lrecl), filesize % te->lrecl);
93 snprintf(pbuf, 100, "rec %03x at %p\n", rec,
94 buf + (rec * te->lrecl));
95 wto(pbuf);
96 append_record(&fst, buf2);
100 static u32 getnumber(u8 *data, int digits)
102 u32 ret = 0;
104 for(;digits; digits--, data++)
105 ret = (ret * 8) + (*data - '0');
107 return ret;
110 static void readcard(u8 *buf)
112 static int eof;
113 int ret;
114 struct ccw ccw;
116 if (eof)
117 return;
119 ccw.cmd = 0x02;
120 ccw.flags = 0;
121 ccw.count = 80;
122 ccw.addr = ADDR31(buf);
124 ORB.param = 0x12345678,
125 ORB.f = 1,
126 ORB.lpm = 0xff,
127 ORB.addr = ADDR31(&ccw);
129 ret = __do_io(ipl_sch);
130 if (ret == 0x01) {
131 eof = 1;
132 return; // end of media
135 if (ret)
136 die();
139 void unload_archive(void)
141 char printbuf[132];
142 struct cpio_hdr *hdr;
143 u8 *dasd_buf;
144 int save;
145 int fill;
146 int i;
148 u32 filesize;
149 u32 namesize;
151 dasd_buf = malloc(2*1024*1024);
152 hdr = (void*) dasd_buf;
154 wto("\n");
156 fill = 0;
157 while(1) {
158 /* read a file header */
159 if (fill < sizeof(struct cpio_hdr)) {
160 readcard(dasd_buf + fill);
161 fill += 80;
164 namesize = getnumber(hdr->namesize, 6);
165 filesize = getnumber(hdr->filesize, 11);
167 while(namesize + sizeof(struct cpio_hdr) > fill) {
168 readcard(dasd_buf + fill);
169 fill += 80;
172 if ((namesize == 11) &&
173 !strncmp("TRAILER!!!", (char*) hdr->data, 10))
174 break;
176 save = 0;
177 for(i=0; table[i].lrecl != -1; i++) {
178 if (!strcmp((char*) hdr->data, table[i].arch)) {
179 save = 1;
180 break;
184 if (save) {
185 snprintf(printbuf, 132, "processing '%.8s' '%.8s' => '%s'\n",
186 table[i].fn, table[i].fn + 8, (char*) hdr->data);
187 wto(printbuf);
188 } else {
189 snprintf(printbuf, 132, "skipping '%s'\n",
190 (char*) hdr->data);
191 wto(printbuf);
194 fill -= (sizeof(struct cpio_hdr) + namesize);
195 memmove(hdr, hdr->data + namesize, fill);
197 /* read the entire file into storage (assuming it's <= 1MB) */
198 while(fill < filesize) {
199 readcard(dasd_buf + fill);
200 fill += 80;
203 if (save)
204 save_file(&table[i], filesize, dasd_buf);
206 fill -= filesize;
207 memmove(dasd_buf, dasd_buf + filesize, fill);