forked from gligli/xell
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcdrom.c
376 lines (307 loc) · 7.41 KB
/
cdrom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <vsprintf.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define LOADER_RAW 0x8000000004000000ULL
#define LOADER_MAXSIZE 0x1000000
#define ATAPI_ERROR 1
#define ATAPI_DATA 0
#define ATAPI_DRIVE_SELECT 6
#define ATAPI_BYTE_CNT_L 4
#define ATAPI_BYTE_CNT_H 5
#define ATAPI_FEATURES 1
#define ATAPI_COMMAND 7
#define ATAPI_STATUS 7
#define ATAPI_STATUS_DRQ 8
#define ATAPI_STATUS_ERR 1
extern char getchar(void);
void ide_out8(int reg, unsigned char d)
{
*(volatile unsigned char*)(0x80000200ea001200ULL + reg) = d;
}
void ide_out16(int reg, unsigned short d)
{
*(volatile unsigned short*)(0x80000200ea001200ULL + reg) = d;
}
void ide_out32(int reg, unsigned int d)
{
*(volatile unsigned int*)(0x80000200ea001200ULL + reg) = d;
}
unsigned char ide_in8(int reg)
{
return *(volatile unsigned char*)(0x80000200ea001200ULL + reg);
}
unsigned int ide_in32(int reg)
{
return *(volatile unsigned int*)(0x80000200ea001200ULL + reg);
}
int ide_waitready()
{
while (1)
{
int status=ide_in8(ATAPI_STATUS);
if ((status&0xC0)==0x40) // RDY and /BUSY
break;
if (status == 0xFF)
return -1;
}
return 0;
}
int atapi_packet(unsigned char *packet, unsigned char *data, int data_len)
{
ide_out8(ATAPI_DRIVE_SELECT, 0xA0);
data_len /= 2;
ide_out8(ATAPI_BYTE_CNT_L, data_len & 0xFF);
ide_out8(ATAPI_BYTE_CNT_H, data_len >> 8);
ide_out8(ATAPI_FEATURES, 0);
ide_out8(ATAPI_COMMAND, 0xA0);
if (ide_waitready())
{
printf(" ! waitready after command failed!\n");
return -2;
}
if (ide_in8(ATAPI_STATUS) & ATAPI_STATUS_DRQ)
{
ide_out32(ATAPI_DATA, *(unsigned int*)(packet + 0));
ide_out32(ATAPI_DATA, *(unsigned int*)(packet + 4));
ide_out32(ATAPI_DATA, *(unsigned int*)(packet + 8));
} else if (ide_in8(ATAPI_STATUS) & ATAPI_STATUS_ERR)
{
printf(" ! error before sending cdb\n");
return -3;
} else
{
printf(" ! DRQ not set\n");
return -4;
}
if (ide_waitready())
{
printf(" ! waitready after CDB failed\n");
return -5;
}
while (data_len)
{
unsigned char x = ide_in8(ATAPI_STATUS);
if (x & 0x80) // busy
continue;
if (x & 1) // err
{
printf(" ! ERROR after CDB [%02x], err=%02x.\n", data[0], ide_in8(ATAPI_ERROR));
break;
}
*(unsigned int*)data = ide_in32(ATAPI_DATA);
data_len -= 2;
data += 4;
}
if (ide_in8(ATAPI_STATUS) & ATAPI_STATUS_ERR)
{
printf(" ! error after reading response\n");
return -6;
}
return 0;
}
int request_sense(void)
{
unsigned char request_sense[12] = {0x03, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0};
unsigned char sense[24];
if (atapi_packet(request_sense, sense, 24))
{
printf(" ! REQUEST SENSE failed\n");
return -1;
}
return (sense[2] << 16) | (sense[12] << 8) | sense[13];
}
int read_sector(unsigned char *sector, int lba, int num)
{
int maxretries = 10;
retry:
if (!maxretries--)
{
printf(" ! sorry.\n");
return -1;
}
unsigned char read[12] = {0x28, 0, lba >> 24, lba >> 16, lba >> 8, lba, 0, num >> 8, num, 0, 0, 0};
if (atapi_packet(read, sector, 0x800 * num))
{
printf(" ! read sector failed!\n");
int sense = request_sense();
printf(" ! sense: %06x\n", sense);
if ((sense >> 16) == 6)
{
printf(" * UNIT ATTENTION -> just retry\n");
goto retry;
}
if (sense == 0x020401)
{
printf(" ! not ready -> retry\n");
delay(1);
goto retry;
}
return -1;
}
return 0;
}
void set_modeb(void)
{
unsigned char res[2048];
unsigned char modeb[12] = {0xE7, 0x48, 0x49, 0x54, 0x30, 0x90, 0x90, 0xd0, 0x01};
printf("atapi_packet result: %d\n", atapi_packet(modeb, res, 0x30));
}
struct pvd_s
{
char id[8];
char system_id[32];
char volume_id[32];
char zero[8];
unsigned int total_sector_le, total_sect_be;
char zero2[32];
unsigned int volume_set_size, volume_seq_nr;
unsigned short sector_size_le, sector_size_be;
unsigned int path_table_len_le, path_table_len_be;
unsigned int path_table_le, path_table_2nd_le;
unsigned int path_table_be, path_table_2nd_be;
unsigned char root_direntry[34];
// ...
} __attribute__((packed));
static unsigned char sector_buffer[0x800];
/* really simple. */
int memicmp(void *a, void *b, int len)
{
unsigned char *_a = a, *_b = b;
while (len--)
if (toupper(*_a++) != toupper(*_b++))
return 1;
return 0;
}
int read_direntry(unsigned char *direntry, char *filename, int *lba, int *size)
{
int nrb = *direntry++;
++direntry;
direntry += 4;
*lba = (*direntry++) << 24;
*lba |= (*direntry++) << 16;
*lba |= (*direntry++) << 8;
*lba |= (*direntry++);
direntry +=4;
*size = (*direntry++) << 24;
*size |= (*direntry++) << 16;
*size |= (*direntry++) << 8;
*size |= (*direntry++);
direntry += 7; // skip date
int flags = *direntry++;
++direntry; ++direntry; direntry+=4;
int nl = *direntry++;
int fnl = strlen(filename);
if (!fnl)
fnl++;
/* printf("compare <%s> (%d) with <%s> (%d) [%02x %02x] vs [%02x %02x]\n", filename, fnl, direntry, nl, direntry[0], direntry[1], filename[0], filename[1]); */
if ((nl != fnl) || (memicmp(direntry, filename, nl)))
{
printf("no\n");
*lba = 0;
*size = 0;
}
return nrb;
}
int read_directory(int sector, int len, char *filename, int *lba, int *size)
{
if (read_sector(sector_buffer, sector, 1))
{
printf("read_directory: read sector failed\n");
return -1;
}
int ptr = 0;
for (ptr =0 ; ptr < 0x40; ++ptr)
{
printf("%02x ", sector_buffer[ptr]);
if ((ptr&15)==15)
printf("\n");
}
ptr = 0;
while (len > 0)
{
printf("At %08x\n", ptr);
ptr += read_direntry(sector_buffer + ptr, filename, lba, size);
if (*lba || *size)
return 0;
if (!sector_buffer[ptr])
{
len -= 2048;
read_sector(sector_buffer, ++sector, 1);
ptr = 0;
}
}
printf("file not found!\n");
return -2;
}
extern int execute_elf_at(void *dst, int len, const char *args);
int iso9660_load_file(char *filename, void* addr)
{
int sector, size, file_sector, file_size;
char fnamebuf[258];
struct pvd_s *pvd = 0;
printf(" * reading CD/DVD...\n");
if (read_sector(sector_buffer, 16, 1))
{
printf(" ! failed to read PVD\n");
return -1;
}
pvd = (void*)sector_buffer;
if (memcmp(sector_buffer, "\1CD001\1", 8))
{
printf(" ! no iso9660!\n");
return -1;
}
printf("root_direntry offset: %d\n", (int)(&((struct pvd_s *)0)->root_direntry));
read_direntry(pvd->root_direntry, "", §or, &size);
if (!sector)
{
printf(" ! root direntry not found\n");
return -1;
}
printf(" ! root at lba=%02x, size=%d\n", sector, size);
strcpy(fnamebuf, filename);
strcat(fnamebuf, ";1");
if (read_directory(sector, size, fnamebuf, &file_sector, &file_size))
{
strcpy(fnamebuf, filename);
strcat(fnamebuf, ".;1");
if (read_directory(sector, size, fnamebuf, &file_sector, &file_size))
{
printf(" ! '%s' not found\n", filename);
return -1;
}
}
printf(" ! found '%s' at lba=%d, size=%d\n", fnamebuf, file_sector, file_size);
printf(" ! loading file...\n");
int s = file_size;
while (1)
{
printf("\r * %08x -> %p... ", file_sector, addr);
int num = (s + 0x7ff) / 0x800;
if (num > 64)
num = 64;
if (read_sector(addr, file_sector, num))
{
printf("\n ! read sector failed!\n");
break;
}
addr += num * 0x800; file_sector+=num; s -= num * 0x800;
if (s <= 0)
{
printf("\n * done!\n");
return file_size;
}
}
return -1;
}
void try_boot_cdrom(char *filename)
{
int size;
set_modeb();
if (!filename)
filename = "vmlinux";
if ((size = iso9660_load_file(filename, (void*)LOADER_RAW)) >= 0) {
execute_elf_at((void*)LOADER_RAW, size, "");
}
}