-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso9660.inc
717 lines (594 loc) · 19.3 KB
/
iso9660.inc
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;;
;; Distributed under terms of the GNU General Public License. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$Revision: 7136 $
DESCRIPTOR_TYPE_BOOT = 0x00
; äëÿ äðàéâåðà áåñïîëåçíûé äåñêðèïòîð, ñêèïàåì
DESCRIPTOR_TYPE_PRIMARY = 0x01
; +8 : rb 32 - strA èìÿ ñèñòåìû, êîòîðàÿ ìîæåò âçàèìîäåéñòâîâàòü íà ñåëåêòîðû 0-0F òîìà
; +40 : rb 32 - strD èäåíòèôèêàöèÿ òîìà
; +80 : rb 4 - int32 êîëè÷åñòâî ëîãè÷åñêèõ áëîêîâ, â êîòîðûå çàïèñàí òîì
; +120: rb 2 - int16 ðàçìåð íàáîðà â ýòîì ëîãè÷åñêîì òîìå (êîëè÷åñòâî äèñêîâ)
; +124: rb 2 - int16 íîìåð ýòîãî äèñêà â íàáîðå òîìîâ
; +128: rb 2 - int16 ðàçìåð ëîãè÷åñêîãî áëîêà â áàéòàõ. Îí íå âñåãäà 2048 ìîæåò áûòü!
; +132: rb 4 - int32 ðàçìåð òàáëèöû ïóòåé â áàéòàõ
; +140: rb 4 - int32 LBA àäðåñ òàáëèöû ïóòåé
; +144: rb 4 - int32 LBA àäðåñ íåîáÿçàòåëüíîé òàáëèöû ïóòåé
; +156: rb 34 - Çàïèñü êîðíåâîãî êàòàëîãà èìåííî çàïèñü!!! íå LBA
DESCRIPTOR_TYPE_SUPPLEMENTARY = 0x02
; òîæå ÷òî è TYPE_PRIMARY, íî äëÿ ðàñøèðåíèÿ Juliet(UTF-16)
DESCRIPTOR_TYPE_PARTITION = 0x02
DESCRIPTOR_TYPE_TERMANATOR = 0xff
; Ïóñòîé äåñêðèïòîð, êîòîðûé îñòàíàâëèâàåò ïîèñê
struct ISO9660_VOLUME_DESCRIPTOR
type rb 1
magic rb 5 ; 'CD001'
version rb 1
ends
struct ISO9660_PRIMARY_DESCRIPTOR ISO9660_VOLUME_DESCRIPTOR
rb 1
rb 32
VolumeName rb 32 ; ascii or utf-16
rb 4
VolumeSpaceSize rb 8
rb 32
rb 4 ; +120
rb 4
LBA_Size rb 4 ; +128
PathTableSize rb 8 ; Do not use it!!!
LBA_PathTable rb 4 ; +140 ; Do not use it!!!
rb 4 ; +144 ; Do not use it!!!
rb 8
root_dir_record rb 34 ;TODO
rb 128*4 + 37*3
date_created rb 17
date_modiffed rb 17
rb 17 ; ïðîòóõàíèå ôàéëà
file_struct_ver rb 1 ;always 0x01
ends
; ñòðóêòóðà çàïèñè äèðåêòîðèè
; +0 : rb 1 - size struct
; +1 : rb 1 - óâåëè÷åííàÿ äëèíà çàïèñè àòðèáóòà
; +2 : rb 4 - LBA
; +10 : rb 4 - size
; +18 : rb 7 - date and time
; +25 : rb 1 - flags
; - 1b 'ñêðûòûé'
; - 10b äèðåêòîðèÿ
; +32 : rb 1 - name length
; +33 : rb ? - name
struct ISO9660_DIRECTORY_RECORD
size rb 1
rb 1
lba rd 1
rd 1
data_length rd 1
rd 1
date_time rb 7
flags rb 1
rb 2+4
name_len rb 1
name rb 1 ; rb [name_len]
ends
; Internal data for every ISO9660 partition.
struct ISO9660 PARTITION
type_encoding rd 1 ; 0 - ascii 1 - UCS-2
lba_size rd 1 ; default 2048 TODO
primary_descr rd 1 ; sector num
root_dir_lba rd 1 ; lba32
root_dir_len rd 1 ;
ends
; FAT external functions
; in:
; ebx -> parameter structure of sysfunc 70
; ebp -> ISO9660 structure
; esi -> path string in UTF-8
; out:
; eax, ebx = return values for sysfunc 70
iglobal
align 4
iso9660_user_functions:
dd iso9660_free
dd (.end - $) / 4
dd iso9660_Read
dd iso9660_ReadFolder
dd 0
dd 0
dd 0
dd iso9660_GetFileInfo
.end:
endg
; mount if it's a valid ISO9660 partition
iso9660_create_partition:
; in:
; ebp -> PARTITION structure
; ebx -> boot sector
; ebx + 2048 -> buffer
; esi -> DISK structure
; out:
; eax -> iso9660 partition structure, 0 = not iso9660
cmp dword [esi + DISK.MediaInfo.SectorSize], 2048 ; cd disks
jnz .fail_disk_sector
push ebx
sub esp, 4*3 ; locals value: primary_lba, encoding, select_sector
mov dword[esp], 16-1 ; sector number
mov dword[esp + 4], 0 ; base encoding - ascii
add ebx, 2048
.new_descr:
inc dword[esp]
; ÷èòàåì 16 ñåêòîð, ñìîòðèì çàãîëîâîê äåñêðèïòîðà
xor edx, edx
mov eax, [esp]
mov ecx, 1
; ebx - buffer
; edx:eax - num sector
; ebp - PARTITION
; ecx - number sectors
call fs_read64_sys
test eax, eax
jnz .err_disk_1
cmp dword[ebx + 1], 'CD00'
jnz .err_disk_1
cmp byte[ebx + 5], '1'
jnz .err_disk_1
cmp byte[ebx], DESCRIPTOR_TYPE_TERMANATOR
jz .end_scan
cmp byte[ebx], DESCRIPTOR_TYPE_PRIMARY
jnz @f
cmp dword[esp + 4], 0
jnz .new_descr
mov dword[esp + 4], 0 ; set UCS-2 encoding
mov eax, [esp]
mov dword[esp + 8], eax ; set lba address
jmp .new_descr
@@:
cmp byte[ebx], DESCRIPTOR_TYPE_SUPPLEMENTARY
jnz .new_descr
mov dword[esp + 4], 1 ; set UCS-2 encoding
mov eax, [esp]
mov dword[esp + 8], eax ; set lba address
jmp .new_descr
.end_scan:
; get root dir lba, root dir size, size lba
xor edx, edx
mov eax, [esp + 8]
mov ecx, 1
call fs_read64_sys
test eax, eax
jnz .err_disk_1
; alloc memory for ISO9660 struct
mov eax, sizeof.ISO9660
call malloc
test eax, eax
jz .err_disk_1
; copy data on struct
add esp, 4
pop dword[eax + ISO9660.type_encoding]
mov dword[eax + Iso9660.lba_size], 2048 ;TODO
pop dword[eax + ISO9660.primary_descr]
mov ecx, dword[ebx + ISO9660_PRIMARY_DESCRIPTOR.root_dir_record\
+ ISO9660_DIRECTORY_RECORD.lba]
mov [eax + ISO9660.root_dir_lba], ecx
mov ecx, dword[ebx + ISO9660_PRIMARY_DESCRIPTOR.root_dir_record\
+ ISO9660_DIRECTORY_RECORD.data_length]
mov [eax + ISO9660.root_dir_len], ecx
push edi esi
mov edi, eax
mov esi, ebp
mov ecx, sizeof.PARTITION/4
rep movsd
pop esi edi
pop ebx
ret
.err_disk_1:
add esp, 4*3
pop ebx
.fail_disk_sector:
xor eax, eax
ret
; IN: eax - ptr PARTITION
; OUT: -
; SAVE: esi, edi
; Function free PARTITION struct and all object this structure
iso9660_free:
jmp free
; ISO9660 external functions
; in:
; ebx -> parameter structure of sysfunc 70
; ebp -> ISO9660 structure
; esi -> path string in UTF-8
; out:
; eax, ebx = return values for sysfunc 70
iso9660_Read:
sub esp, 4 ; for ptr on memory page
call iso9660_find_file
mov esi, eax
mov edi, [ebx + 16] ; ptr to programm buffer
test byte[esi + ISO9660_DIRECTORY_RECORD.flags], 10b ; check dir
jnz iso9660_find_file.not_found
; check offset (offset <= size)
mov eax, [ebx + 4] ; low offset
mov edx, eax
cmp [ebx + 8], 0 ; high offset
jnz iso9660_find_file.bad_offset ; error offset > max size
add eax, [ebx + 12] ;size copy data
jc iso9660_find_file.bad_offset ; error offset + size > max size
cmp edx, [esi + ISO9660_DIRECTORY_RECORD.data_length]
jae iso9660_find_file.bad_offset ; error offset > file length
; good file - copy file data
sub esp, 4*4 ; esp+3*4 = fs err code esp+2*4= size copy data
mov dword[esp + 3*4], 0 ; esp+1*4 = offset copy esp = count copying data
mov [esp + 2*4], eax ; copy finish offset
sub [esp + 2*4], edx ; = size copy data
mov [esp + 1*4], edx ; offset to start copy
mov [esp], 0 ; count copping byte
; check end offset (offset+size_buff <= size)
cmp eax, [esi + ISO9660_DIRECTORY_RECORD.data_length]
jbe .full_size ; offset+size_buff <= file length
; read offset - (file size - offset)
mov dword[esp + 3*4], 6
mov eax, [esi + ISO9660_DIRECTORY_RECORD.data_length]
sub eax, edx ;[esp + 1*4]
mov dword[esp + 2*4], eax ; = size copy data
.full_size:
; check offset mod sector_size = 0
test edx, not -2048
jz .first_align ; no creat buffer for first align
push edx
stdcall kernel_alloc, 4096 ; TODO
mov ebx, eax
pop edx
test eax, eax
jz .err_memory
; read sector
push edx ebx
and edx, -2048
shr edx, BSF 2048
mov eax, [esi + ISO9660_DIRECTORY_RECORD.lba]
add eax, edx
mov ecx, 1
xor edx, edx
; ebx - buffer
; edx:eax - num sector
; ebp - PARTITION
; ecx - number sectors
call fs_read64_sys
pop ebx edx
test eax, eax
jnz .err_disk_1
not edx
and edx, not -2048
add dword[esp + 1*4], not -2048
and dword[esp + 1*4], -2048
cmp dword[esp + 2*4], edx
jb @f
mov edx, [esp + 2*4]
@@:
sub dword[esp + 2*4], edx
add dword[esp], edx
push esi
; copy (2048 - offset) and -2048
mov esi, ebx ; buffer
mov ecx, edx
rep movsb
pop esi
stdcall kernel_free, ebx
.first_align:
mov ecx, [esp + 2*4]
and ecx, -2048
cmp ecx, 2048
jb .copy_finish_block
mov eax, [esp + 1*4]
shr eax, BSF 2048
; copy main block
mov ebx, edi
add edi, ecx
sub dword[esp + 2*4], ecx
add dword[esp + 1*4], ecx
add dword[esp], ecx
shr ecx, BSF 2048
xor edx, edx
add eax, [esi + ISO9660_DIRECTORY_RECORD.lba]
; ebx - buffer
; edx:eax - num sector
; ebp - PARTITION
; ecx - number sectors
call fs_read64_sys
test eax, eax
jnz .err_disk
.copy_finish_block:
cmp dword[esp + 2*4], 0
jz .end_align ; creat buffer for end read sector
stdcall kernel_alloc, 4096
mov ebx, eax
jz .err_memory
;copy finish block
mov eax, [esp + 1*4]
shr eax, BSF 2048
xor edx, edx
mov ecx, 1
add eax, [esi + ISO9660_DIRECTORY_RECORD.lba]
; ebx - buffer
; edx:eax - num sector
; ebp - PARTITION
; ecx - number sectors
call fs_read64_sys
test eax, eax
jnz .err_disk_1
mov esi, ebx
mov ecx, [esp + 2*4]
add dword[esp], ecx
rep movsb
stdcall kernel_free, ebx
.end_align:
; set ebx size copy data
mov ebx, [esp]
mov esi, [esp + 3*4]
add esp, 4*4
call kernel_free
mov eax, esi
ret
.err_disk_1:
stdcall kernel_free, ebx
.err_disk:
add esp, 4*4
call kernel_free
xor ebx, ebx
mov eax, 11 ; device error
ret
.err_memory:
add esp, 4*4
call kernel_free
xor ebx, ebx
mov eax, 12
ret
; FAT external functions
; in:
; ebx -> parameter structure of sysfunc 70
; ebp -> ISO9660 structure
; esi -> path string in UTF-8
; out:
; eax, ebx = return values for sysfunc 70
iso9660_ReadFolder:
sub esp, 4 ; for ptr on memory page
call iso9660_find_file
test byte[eax + ISO9660_DIRECTORY_RECORD.flags], 10b ; check dir
jz iso9660_find_file.not_found
; create buffer for read sectors
; check index file in dir
; loop copy file info and name
.new_file:
call iso9660_GetFileInfo.copy_file_info
; copy encoding
; copy name
; free buffer
ret
; ISO9660 external functions
; in:
; ebx -> parameter structure of sysfunc 70
; ebp -> ISO9660 structure
; esi -> path string in UTF-8
; out:
; eax, ebx = return values for sysfunc 70
iso9660_GetFileInfo:
cmp byte[esi], 0
jz .rootdir
sub esp, 4 ; for ptr on memory page
call iso9660_find_file
mov ecx, [ebx + 16] ; buffer
call .copy_file_info
call kernel_free
xor eax, eax
mov ebx, 40
ret
; IN: eax -> ISO966_DIRECTORY_RECORD
; ecx -> buffer
; destruct: edx
.copy_file_info:
; copy flags(dir of file)
xor edx, edx
or dl, 100001b
test byte[eax + ISO9660_DIRECTORY_RECORD.flags], 1b ; check hidden flag
jz @f
or dl, 10b
@@:
test byte[eax + ISO9660_DIRECTORY_RECORD.flags], 10b ; check dir
jz @f
and dl, not 1b
or dl, 10000b ; dir flag
@@:
mov [ecx], edx
; copy size
mov [ecx + 36], 0
mov edx, [eax + ISO9660_DIRECTOEY_RECORD.data_length]
mov [ecx + 32], edx
; copy date creat file
movzx edx, byte[eax + ISO9660_DIRECTOEY_RECORD.date_time]
add edx, 1900 ; year
shl edx, 8
mov dl, byte[eax + ISO9660_DIRECTOEY_RECORD.date_time + 1] ;month
shl edx, 8
mov dl, byte[eax + ISO9660_DIRECTOEY_RECORD.date_time + 2] ;day
mov [ecx + 12], edx
mov [ecx + 20], edx
mov [ecx + 28], edx
; copy time creat file
movzx edx, byte[eax + ISO9660_DIRECTOEY_RECORD.date_time + 3] ;hour
shl edx, 8
mov dl, byte[eax + ISO9660_DIRECTOEY_RECORD.date_time + 4] ; minute
shl edx, 8
mov dl, byte[eax + ISO9660_DIRECTOEY_RECORD.date_time + 5] ; second
mov [ecx + 8], edx
mov [ecx + 16], edx
mov [ecx + 24], edx
ret
.rootdir:
mov edi, [ebx + 16] ; edi = buffer
; copy flags (dir)
mov byte [edi], 8
; copy size drive
mov eax, dword[ebp + PARTITION.Length+DQ.lo]
mov edx, dword[ebp + PARTITION.Length+DQ.hi]
mov ecx, [ebp + PARTITION.Disk]
mov ecx, [ecx + DISK.MediaInfo.SectorSize]
bsf ecx, ecx
shld edx, eax, cl
shl eax, cl
mov [edi + 32], eax ; bdfe.size.lo
mov [edi + 36], edx ; bdfe.size.hi
mov eax, [ebx + 8]
; copy encoding
mov [edi + 4], eax
; check encoding on fs struct
test eax, eax ; check f70s5arg.xflags
jz .no_name
; stdcall is_region_userspace, edi, ecx
; alloc memory for read primary descriptor
stdcall kernel_alloc, PAGE_SIZE
test eax, eax
jz .no memory
push eax
mov esi, eax
; read primary descriptor
mov ebx, eax
mov ecx, 1
xor edx, edx
mov eax, [ebp + ISO9660.primary_descr]
; ebx - buffer
; edx:eax - num sector
; ebp - PARTITION
; ecx - number sectors
call fs_read64_sys
test eax, eax
jnz .err_read_part
add esi, ISO9660_PRIMARY_DESCRIPTOR.VolumeName
mov edx, [edi + 4]
add edi, 40 ; offset partition name
mov ecx, 32
call iso9660_copy_name
; free memory
call kernel_free
.no_name:
xor eax, eax
mov ebx, 40
ret
.err_read_part:
call kernel_free
mov eax, 11
ret
.no_memory:
mov eax, 12
ret
;; private function
; IN: esi - ptr string UTF-8 or zero for root directory
; OUT: eax - ptr to directory record
; [esp + 4] - ptr to memory page for destruct
iso9660_find_file:
cmp byte[esi], 0
jz .rootdir
.next_dir:
.next_sector:
;get sector directory
; find name
.found:
; finish name?
; no: jmp .next_dir
; malloc memory for dir record
; copy data
ret
.rootdir:
;get primary discriptor
; malloc memory for dir record
; copy data
ret
; errors
.not_found_1:
add esp, 4
.not_found: ; esp - ptr to page
call kernel_free
mov eax, 5
xor ebx, ebx
ret
.bad_offset: ; esp - ptr to page
call kernel_free
mov eax, 6
xor ebx, ebx
ret
.no_memor_1:
add esp, 4
.no_memory:
mov eax, 12
xor ebx, ebx
ret
;IN: ebp -> PARTITION structure
; ebx -> FS structure
; esi -> input string (ascii or ucs2)
; ecx = count bytes
; edi -> output string (buffer)
; edx = encoding output string
;OUT: edi increasing
; esi increasing
; ecx = 0 or other value
iso9660_copy_name:
cmp [ebp + ISO9660.type_encoding], 0
jnz .ucs2
cmp edx, 1 ; cp866 encoding
jne @f
; no convert chars
rep movsb
jmp .no_name
@@:
cmp edx, 2 ; utf16 encoding
jne .ascii2utf8
; convert ascii to utf16LE
@@:
lodsb
call ansi2uni_char
stosw
loop @b
jmp .end_copy_name
.ascii2utf8:
cmp edx, 3 ; utf8 encoding
jne .end_copy_name
; convert ascii to utf8
call cp866toUTF8_string
jmp .end_copy_name
.ucs2:
shr ecx, 1 ; ecx/2 - ucs2 chars
cmp edx, 1 ; cp866 encoding
jne .ucs2utf16
; convert ucs2 to ascii
@@:
lodsw
xchg al, ah
call uni2ansi_char
stosb
loop @b
jmp .end_copy_name
.ucs2utf16:
cmp edx, 2 ; utf16 encoding
jne .ucs2utf8
; convert ucs2 to utf16LE
@@:
lodsw
xchg al, ah
stosw
loop @b
jmp .end_copy_name
.ucs2utf8:
cmp edx, 3 ; utf8 encoding
jne .end_copy_name
; convert ucs2 to utf8
mov ebx, ecx
shl ecx, 1
@@:
lodsw
xchg ah, al
call UTF16to8
dec ebx
jnz @b
.end_copy_name:
ret