Chameleon

Chameleon Commit Details

Date:2013-12-02 20:02:11 (5 years 6 months ago)
Author:Jief Luce
Commit:2279
Parents: 2278
Message:Relocate the 2 sectors. Avoid a bug if you call a subroutine in the 2nd sector (like getc).
Changes:
M/branches/Jief/i386/boot1/boot1h.s

File differences

branches/Jief/i386/boot1/boot1h.s
11
22
33
4
4
55
66
77
......
99
1010
1111
12
12
1313
1414
1515
......
1717
1818
1919
20
20
2121
2222
2323
......
2525
2626
2727
28
28
2929
3030
3131
......
7070
7171
7272
73
7374
7475
7576
......
8788
8889
8990
90
91
9192
9293
9394
......
266267
267268
268269
269
270
270271
271272
272273
......
336337
337338
338339
339
340
340341
341342
342343
......
361362
362363
363364
364
365
365366
366367
367368
......
384385
385386
386387
387
388388
389
389390
390391
391392
392393
393394
394395
395
396396
397397
398398
399
399
400400
401
402401
403402
404403
405404
406405
407406
408
409407
408
409
410
410411
411412
412413
413
414
415
414
415
416
416417
417418
418419
419
420
420
421
421422
422423
423424
424425
425426
426
427
428
429
430
431
432
427
428
429
430
431
432
433
433434
434435
435436
436437
437
438
439
440
438
439
440
441
442
441443
442
444
443445
444446
445447
......
449451
450452
451453
452
454
453455
454456
455457
......
464466
465467
466468
467
469
468470
469471
470472
......
502504
503505
504506
505
507
506508
507509
508510
509511
510
512
511513
512514
513
515
514516
515517
516518
......
523525
524526
525527
526
528
527529
528530
529531
......
559561
560562
561563
562
564
563565
564566
565567
......
669671
670672
671673
672
674
673675
674676
675677
......
748750
749751
750752
751
753
752754
753755
754756
......
762764
763765
764766
765
766
767
768
769
770
771
772
773
774
775767
776768
777769
......
809801
810802
811803
812
804
813805
814806
815807
......
878870
879871
880872
881
873
882874
883875
884876
885877
886
878
887879
888880
889
881
890882
891883
892884
......
949941
950942
951943
952
944
953945
954946
955
947
956948
957949
958950
......
961953
962954
963955
956
957
958
959
960
961
962
963
964964
965965
966966
......
10081008
10091009
10101010
1011
1012
1013
1014
1015
1016
1017
1018
10191011
10201012
10211013
......
10291021
10301022
10311023
1032
1024
10331025
10341026
10351027
10361028
10371029
10381030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
10391041
10401042
10411043
......
10941096
10951097
10961098
1097
1099
10981100
10991101
11001102
1101
1103
11021104
11031105
11041106
......
11091111
11101112
11111113
1112
1114
11131115
11141116
1115
1117
11161118
11171119
1118
1120
11191121
11201122
11211123
1122
1124
11231125
11241126
11251127
11261128
1127
1129
11281130
11291131
11301132
......
11351137
11361138
11371139
1138
1140
11391141
11401142
11411143
......
11511153
11521154
11531155
1154
1156
11551157
11561158
11571159
......
12191221
12201222
12211223
1222
1224
12231225
12241226
12251227
12261228
12271229
12281230
1229
1230
12311231
12321232
12331233
......
12351235
12361236
12371237
1238
1238
12391239
12401240
12411241
12421242
12431243
12441244
1245
1246
1245
1246
12471247
12481248
12491249
12501250
1251
12521251
12531252
12541253
1255
12561254
1257
12581255
1259
12601256
12611257
12621258
......
12661262
12671263
12681264
1269
1265
12701266
12711267
12721268
......
13361332
13371333
13381334
1339
1335
13401336
13411337
13421338
......
13641360
13651361
13661362
1367
1363
13681364
13691365
13701366
13711367
13721368
1373
1369
13741370
13751371
13761372
13771373
13781374
13791375
1380
1376
13811377
13821378
13831379
; Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
;
; @APPLE_LICENSE_HEADER_START@
;
;
; Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
; Reserved. This file contains Original Code and/or Modifications of
; Original Code as defined in and that are subject to the Apple Public
; except in compliance with the License. Please obtain a copy of the
; License at http://www.apple.com/publicsource and read it before using
; this file.
;
;
; The Original Code and all software distributed under the License are
; distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
; EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
; FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
; License for the specific language governing rights and limitations
; under the License.
;
;
; @APPLE_LICENSE_HEADER_END@
;
; Partition Boot Loader: boot1h
; This program is designed to reside in sector 0+1 of an HFS+ partition.
; It expects that the MBR has left the drive number in DL
; and a pointer to the partition entry in SI.
;
;
; This version requires a BIOS with EBIOS (LBA) support.
;
; This code is written for the NASM assembler.
kChameleonBoot1hSignatureEQU0xBB99; boot sector signature
kBootSignatureEQU0xAA55; boot sector signature
kBoot1SegmentEQU 0x0000
kBoot1StackAddressEQU0xFFF0; boot1 stack pointer
kBoot1LoadAddrEQU0x7C00; boot1 load address
kBoot1RelocAddrEQU0xE000; boot1 relocated address
; giving the size of the structure.
;
struc part
;.bootidresb 1; bootable or not
;.bootidresb 1; bootable or not
;.headresb 1; starting head, sector, cylinder
;.sectresb 1;
;.cylresb 1;
.fileModeresw1
.specialresd1
endstruc
;
; FileInfo
;
movsi, %1
callprint_string
%endmacro
%macro LogStringMacro 1
movdi, %1
calllog_string
%else
%define LogString(x)
%endif
;--------------------------------------------------------------------------
; Start of text segment.
mov sp, kBoot1StackAddress ; sp <- top of stack
sti ; reenable interrupts
mov ds, ax ; ds <- 0
mov es, ax ; es <- 0
mov ds, ax ; ds <- 0
push ecx ; save the current partition LBA offset. will be poped after reloc
;
; Relocate boot1 code.
;
pushsi
movsi, kBoot1LoadAddr; si <- source
movdi, kBoot1RelocAddr; di <- destination
cld; auto-increment SI and/or DI registers
movcx, kSectorBytes; copy 256 words
movcx, kSectorBytes*2; copy 2 sectors (boot1 is 2 sectors)
repmovsb; repeat string move (word) operation
popsi
;
; Code relocated, jump to startReloc in relocated location.
;
; FIXME: Is there any way to instruct NASM to compile a near jump
; using absolute address instead of relative displacement?
;
jmpabsstartReloc
;jmpabsstartReloc
jmp kBoot1Segment:start_reloc
;--------------------------------------------------------------------------
; Start execution from the relocated location.
;
startReloc:
PrintChar ('>')
start_reloc:
PrintChar ('1')
PrintChar ('>')
;
; Initializing global variables.
;
pop ecx
;PrintHex(ecx)
pop ecx
;PrintHex(ecx)
mov DWORD [gPartLBA], ecx; save the current partition LBA offset
mov [gBIOSDriveNumber], dl; save BIOS drive number
mov [gBIOSDrivePartNumber], dh; save BIOS drive number
movWORD [gMallocPtr], mallocStart; set free space pointer
PrintHex([gPartLBA])
xor eax,eax
mov al,[gBIOSDriveNumber]
PrintHexEax
xor eax,eax
mov al,[gBIOSDrivePartNumber]
PrintHexEax
; PrintHex([gPartLBA])
; xor eax,eax
; mov al,[gBIOSDriveNumber]
; PrintHexEax
; xor eax,eax
; mov al,[gBIOSDrivePartNumber]
; PrintHexEax
;
; Loading upper 512 bytes of boot1h and HFS+ Volume Header.
;
xorecx, ecx; sector 1 of current partition
incecx
mov al, 2; read 2 sectors: sector 1 of boot1h + HFS+ Volume Header
mov edx, kBoot1Sector1Addr
;xorecx, ecx; sector 1 of current partition
;incecx
mov ecx, 2
mov al, 1; read 2 sectors: sector 1 of boot1h + HFS+ Volume Header
mov edx, kHFSPlusBuffer
call readLBA
;PrintChar ('2')
; PrintChar ('2')
;
; Initializing more global variables.
shreax, 9; convert to sector unit
mov[gBlockSize], eax; save blockSize as little-endian sector unit!
;PrintChar ('3')
; PrintChar ('3')
;
; Looking for HFSPlus ('H+') or HFSPlus case-sensitive ('HX') signature.
; Find stage2 boot file in a HFS+ Volume's root folder.
;
findRootBoot:
PrintChar ('!')
PrintChar ('!')
;%if 0
moval, kHFSCatalogFileID
leasi, [searchCatalogKey]
LogString(root_str)
boot2:
PrintChar ('J')
PrintChar ('J')
mov dl, [gBIOSDriveNumber]; load BIOS drive number
mov dh, [gBIOSDrivePartNumber]; load part number
jmp kBoot2Segment:kBoot2Address
;%endif
;%endif
error:
PrintChar ('E')
PrintChar ('E')
LogString(error_str)
hang:
; Arguments:
; AX = number of 512-byte sectors to read (valid from 1-1280).
; EDX = pointer to where the sectors should be stored.
; ECX = sector offset in partition
; ECX = sector offset in partition
;
; Returns:
; CF = 0 success
; Arguments:
; AL = number of 512-byte sectors to read (valid from 1-127).
; EDX = pointer to where the sectors should be stored.
; ECX = sector offset in partition
; ECX = sector offset in partition
; [bios_drive_number] = drive number (0x80 + unit number)
;
; Returns:
callprint_string
popad
ret
;-------------------------------------------------------------------------
popad
ret
print_nibble:
and al, 0x0f
add al, '0'
%endif ; UNUSED
;--------------------------------------------------------------------------
; getc - wait for a key press
;
getc:
pushad
mov ah, 0
int0x16
popad
ret
;--------------------------------------------------------------------------
; Write a ASCII character to the console.
;
; Arguments:
inccl; increment string length count
cmpal, NULL; check for string terminator
jne.loop
popdi; restore unicode string length pointer
deccl; ignoring terminator from length count
mov[di], cl; save string length
;
compareHFSPlusExtentKeys:
pushad
movdl, 0; DL = result of comparison, DH = bestGuess
moveax, [si + HFSPlusExtentKey.fileID]
cmpeax, [di + HFSPlusExtentKey.fileID]
jne.checkFlags
cmpBYTE [si + HFSPlusExtentKey.forkType], kForkTypeData
jne.checkFlags
moveax, [si + HFSPlusExtentKey.startBlock]
cmpeax, [di + HFSPlusExtentKey.startBlock]
jecompareHFSPlusCatalogKeys.exit
.trialKeyGreater:
decdl
jmp.exit
.searchKeyGreater:
incdl
.exit:
mov[bp + BTree.searchResult], dl
cmpdl, 0; set flags to check relation between keys
ret
;--------------------------------------------------------------------------
; Static data.
;
%if VERBOSE
root_strdb'/boot', NULL
%endif
;--------------------------------------------------------------------------
; Allocate memory
;
; Arguments:
%endif ; UNUSED
;--------------------------------------------------------------------------
; Static data.
;
%if VERBOSE
root_strdb'/boot', NULL
%endif
;--------------------------------------------------------------------------
; Pad the rest of the 512 byte sized sector with zeroes. The last
; two bytes is the mandatory boot sector signature.
;
pad_table_and_sig:
times508-($-$$) db 0
dw kChameleonBoot1hSignature
dwkBootSignature // remove this ! Don't forgot boot0.s, line 419 : -4 become -2
dwkBootSignature ; remove this ! Don't forgot boot0.s, line 419 : -4 become -2
;
; Sector 1 code area
;
;--------------------------------------------------------------------------
; getc - wait for a key press
;
getc:
pushad
mov ah, 0
int0x16
popad
ret
;--------------------------------------------------------------------------
; lookUpBTree - initializes a new BTree instance and
; look up for HFSPlus Catalog File or Extent Overflow keys
;
movbx, [bp + BTree.nodeBuffer + BTNodeDescriptor.numRecords]
xchgbh, bl
decbx
.bsearch:
cmpax, bx
ja.checkResult; jump if lowerBound > upperBound
movcx, ax
addcx, bx
shrcx, 1; test index = (lowerBound + upperBound / 2)
pushad
jl.csearchLessThanTrial
jg.csearchGreaterThanTrial
PrintChar('=')
PrintChar('=')
jmp.csearchCont
.csearchGreaterThanTrial:
PrintChar('>')
PrintChar('>')
jmp.csearchCont
.csearchLessThanTrial:
PrintChar('<')
PrintChar('<')
.csearchCont:
popad
%endif ; UNUSED
.adjustBounds:
je.checkResult
jl.searchLessThanTrial
jg.searchGreaterThanTrial
jmp.bsearch
jmp.bsearch
.searchLessThanTrial:
movbx, cx
movax, cx
incax; lowerBound = index + 1
jmp.bsearch
.checkResult:
cmpBYTE [bp + BTree.searchResult], 0
jge.foundKey
movbx, [bx]
movedx, [bx]
jmp.readNode
.exit:
cmpBYTE [bp + BTree.searchResult], 0
ret
.exit:
popdi; restore address of trialKey
%if UNUSED
%if UNUSED
;
; Print catalog trial key
;
pushad
movsi, di
lodsd
PrintChar('k')
PrintHexEax()
lodsw
cmpax, 0
je.printExit
.printLoop:
lodsw
callprint_char
loop.printLoop
loop.printLoop
.printExit:
popad
;
;
;
%endif ; UNUSED
%if UNUSED
%if UNUSED
;
; Print extent trial key
;
pushad
PrintChar('k')
movsi, di
xoreax, eax
lodsw
PrintHexEax()
lodsd
PrintHexEax()
lodsd
PrintHexEax()
popad
;
;
popsi; restore SI
callbx; call key compare proc
popad
ret
ret
;--------------------------------------------------------------------------
; readExtent - read extents from a HFS+ file (multiple extent support)
popebx
jmp.beginExtentBlock
.continue:
movedx, [di + HFSPlusExtentDescriptor.blockCount]
callblockToSector; ECX = converted current extent's blockCount to sectors
movedx, [di + HFSPlusExtentDescriptor.startBlock]
callblockToSector; ECX = converted to sectors
addecx, eax; file offset converted to sectors
pushsi
movax, si
movedx, [bp + BTree.readBufferPtr]
callreadSectors
popsi
addebx, esi
movax, si
cwde
shlax, 9; convert SI (read sector count) to byte unit
add[bp + BTree.readBufferPtr], eax
sub[bp + BTree.readSize], esi
jz.exit
.nextExtent:

Archive Download the corresponding diff file

Revision: 2279