-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.asm
More file actions
1036 lines (877 loc) · 27 KB
/
Copy pathshell.asm
File metadata and controls
1036 lines (877 loc) · 27 KB
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[BITS 16]
[ORG 0]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; To provide an interface to the user, provides some built-in commands
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro Function_Prologue 0
pusha
push bp
mov bp, sp
%endmacro
%macro Function_Epilogue 0
mov sp, bp
pop bp
popa
%endmacro
mov word[__PROC_LOAD_SEGMENT_NUM__], ds
mov ax, ds
mov ss, ax
mov sp, 0x0fff
call userPrompt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; userPrompt: give a command prompt to the user
;;
userPrompt:
; call setPageColor
; setVideoMode:
; mov ah, 0x00
; mov al, 0x03
; int 0x10
showPrompt:
call setCursor
mov si, PROMPT_STRING
xor cx, cx
mov cl, byte[PROMPT_STRING_SIZE]
call printString
; save current prompt row no
mov ah, [CURR_CURSOR_ROW]
mov [CURR_PROMPT_ROW], ah
;; wait for keypress in a loop
waitForKeyPress:
xor ax, ax
mov ah, 0x10
int 0x16
mov byte[scancode], ah
mov byte[asciicode], al
; asciicode for the pressed key is returned in AL
; and scancode is returned in AH.
; If some special key was pressed call its handler
case1:
cmp ah, byte[RET_KEY]
jz execFileOrCommand
case2:
cmp ah, byte[BACKSPACE_KEY]
jz eraseChar
case3:
cmp ah, byte[UP_KEY]
jz ignoreKey
case4:
cmp ah, byte[DOWN_KEY]
jz ignoreKey
case5:
cmp ah, byte[LEFT_KEY]
jz ignoreKey
case6:
cmp ah, byte[RIGHT_KEY]
jz ignoreKey
case7:
cmp ah, byte[DEL_KEY]
jz ignoreKey
case8:
cmp byte[asciicode], 32
jl ignoreKey
cmp byte[asciicode], 126
jg ignoreKey
defaultcase:
mov di, word[CMDSTR_COUNT]
cmp di, word[MAX_CMDSTR_SIZE]
jz ignoreKey
mov si, word asciicode
xor cx, cx
mov cl, 1
call printString
mov bx, word CMDSTR
mov al, byte[si]
mov byte[ds:bx+di], al
inc word[CMDSTR_COUNT]
jmp waitForKeyPress
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function Data
;;
loop_counter DB 0x00
scancode DB 0x00
asciicode DB 0x00
sp_key_data_size DB 0x00
sp_key_string_size DB 0x00
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;
setCursor:
pusha
xor ax, ax
mov ah, 0x01
mov ch, 00000110b
mov cl, 00000001b
int 0x10
popa
ret
; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;eraseChar: erase last char and move cursor 1 char back
;;
eraseChar:
pusha
mov ax, word[__PROC_LOAD_SEGMENT_NUM__]
mov es, ax
;; if we are trying to erase prompt, ignore keypress
mov al, byte[CURR_CURSOR_ROW]
cmp al, byte[CURR_PROMPT_ROW]
jnz movCursorLeft
mov al, byte[CURR_CURSOR_COL]
cmp al, byte[PROMPT_STRING_SIZE]
jle finish
movCursorLeft:
mov ax, ds
mov es, ax
push 0x0000 ; null byte
mov bp, sp
mov cx, 0x01
mov ah, 0x13
mov al, 0x00
mov bl, 01000000b
mov bh, 0x00
mov dh, [CURR_CURSOR_ROW]
mov dl, [CURR_CURSOR_COL]
cmp dl, 0x00
jz col_n_row
dec dl
int 0x10
jmp saveCurrCursorPosn
col_n_row:
cmp dh, 0x00
jz ignoreEraseReq
mov dl, 79
dec dh
int 0x10
jmp saveCurrCursorPosn
ignoreEraseReq:
jmp finish
saveCurrCursorPosn: ; get current cursor position and save it
;; but first move the cursor 1 left
mov ah, 0x02
mov bh, 0x00
int 0x10
mov ah, 0x03
mov bh, 0x00
int 0x10
mov [CURR_CURSOR_ROW], dh
mov [CURR_CURSOR_COL], dl
finish:
cmp word[CMDSTR_COUNT], 0
jz exit_func
dec word[CMDSTR_COUNT]
exit_func:
add sp, 2
popa
jmp waitForKeyPress
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; local function data
;;
null_byte DB 0x00
nextOperation DW 0x0000
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ignoreKey:
;;
ignoreKey:
jmp waitForKeyPress
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; execFileOrCommand:
;;
execFileOrCommand:
pusha
;print newline
push 0x0A0D
mov si, sp
mov cx, 2
call printString
add sp, 2
;; search a file or command matching the CMDSTRING
COMP_STRINGS:
mov ax, word[ed_cmd_size]
cmp ax, word[CMDSTR_COUNT]
jz MATCH_CHARS
mov ax, word[mkxfs_cmd_size]
cmp ax, word[CMDSTR_COUNT]
jz MATCH_CHARS
jmp CMD_NOT_FOUND_MSG
MATCH_CHARS:
CMD1:
mov di, 0
.loop:
mov bl, byte[ds:CMDSTR+di]
mov cl, byte[ds:ed_cmd+di]
xor bl, cl
cmp bl, 0
jnz CMD2
inc di
cmp di, word[CMDSTR_COUNT]
jnz .loop
call ed
jmp _finish
CMD2:
mov di, 0
.loop1:
mov bl, byte[ds:CMDSTR+di]
mov cl, byte[ds:mkxfs_cmd+di]
xor bl, cl
cmp bl, 0
jnz CMD_NOT_FOUND_MSG
inc di
cmp di, word[CMDSTR_COUNT]
jnz .loop1
call mkxfs
jmp _finish
CMD_NOT_FOUND_MSG:
cmp word[CMDSTR_COUNT], 0
jz _finish
mov si, word CMDSTR
xor cx, cx
mov cx, word[CMDSTR_COUNT]
call printString
mov si, word cmd_not_found_str
xor cx, cx
mov cx, word[cmd_not_found_str_size]
call printString
jmp _finish
_finish:
mov word[CMDSTR_COUNT], 0
popa
jmp showPrompt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;
ed_cmd DB "ed"
ed_cmd_size DW ($-ed_cmd)
mkxfs_cmd DB "mkxfs"
mkxfs_cmd_size DW ($-mkxfs_cmd)
cmd_not_found_str DB ": command not found!", 0x0D, 0x0A
cmd_not_found_str_size DW ($-cmd_not_found_str)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; mkxfs: create a X Filesystem on a primary partition
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; macros definition ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro getData 4
%4:
push word %2
push word ds
push word[ds:%3]
mov bx, ss
mov es, bx
mov bx, sp
mov ax, 4
int 0x26
add sp, 6
mov word[ds:%1], 0
mov word[ds:%1+2], 0
.start:
xor ax, ax
mov ah, 0x10
int 0x16
mov byte[sc_code], ah
mov byte[char_code], al
cmp byte[sc_code], 0x1C ; look for RET (scancode = 0x1C) keypress
jz .end ; data input complete jmp to next location
;; check if character entered is a digit, else ignore
cmp byte[char_code], 48
jl .start
cmp byte[char_code], 57
jg .start
;; print the entered digit on terminal
push word char_code
push word ds
push word 1
mov bx, ds
mov es, bx
mov bx, sp
mov ax, 4
int 0x26
add sp, 6
;; convert to decimal and save in %1
mov ax, word[ds:%1]
mov bx, 10
mul bx
push ax
push dx
mov ax, word[ds:%1+2]
mul bx
pop dx
add dx, ax
pop ax
sub byte[char_code], 48 ; get the digit
xor bx, bx
mov bl, byte[char_code]
add ax, bx ; add to the number
mov word[ds:%1], ax
mov word[ds:%1+2], dx
jmp .start
.SIZE_OVERFLOW:
push word size_overflow_msg
push word ds
push word size_overflow_msg_size
mov bx, ss
mov es, bx
mov bx, sp
mov ax, 4
int 0x26
add sp, 6
.end:
;; print a newline
push word newline_str
push word ds
push word[newline_str_size]
mov bx, ds
mov es, bx
mov bx, sp
mov ax, 4
int 0x26
add sp, 6
nop
%endmacro
%macro invalidValue 3
mov si, word invalid_value_str
mov cx, word[invalid_value_str_size]
call printString
mov si, word %1
mov cx, word[%2]
sub cx, 3
call printString
mov si, word newline_str
mov cx, word[newline_str_size]
call printString
jmp %3
%endmacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mkxfs:
pusha
getData START_SECTOR, ask_start_sector_str, ask_start_sector_str_size, GET_START_SECTOR
mov ax, word[ds:START_SECTOR]
or ax, word[ds:START_SECTOR+2]
cmp ax, 0
jnz GET_PART_SIZE
invalidValue ask_start_sector_str, ask_start_sector_str_size, GET_START_SECTOR
getData PART_SIZE, ask_part_size_str, ask_part_size_str_size, GET_PART_SIZE
mov ax, word[ds:PART_SIZE]
or ax, word[ds:PART_SIZE+2]
cmp ax, 0
jnz GET_CHS
invalidValue ask_part_size_str, ask_part_size_str_size, GET_PART_SIZE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; File System Description ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; block size = 1 sector
; FAT size = 1 block
;
; +-----------+----------+--------------------------------------------------------------+
; | BLOCK NUM | BYTE NUM | DATA |
; +-----------+----------+--------------------------------------------------------------+
; | | 0 - 1 | 0x66BB (Filesystem Signature) |
; | +----------+--------------------------------------------------------------+
; | | 2 - 63 | author ID string followed by at least 1 null byte, |
; | | | remaining space must be filled with null bytes |
; | +----------+--------------------------------------------------------------+
; | | 64 - 66 | 0x3+START_SECTOR (first FAT's block number) (fixed) |
; | 1 +----------+--------------------------------------------------------------+
; | | 67 - 69 | 0x5+START_SECTOR (topmost dir's 1st block num) (fixed) |
; | +----------+--------------------------------------------------------------+
; | | 70 - 72 | 0x7+START_SECTOR (first block of free blocks list) |
; | +----------+--------------------------------------------------------------+
; | | 73 - 75 | (PART_SIZE-1)+START_SECTOR (last block of free blocks list) |
; | +----------+--------------------------------------------------------------+
; | | 76 - 511 | reserved |
; +-----------+----------+--------------------------------------------------------------+
; | 2 | 0 - 511 | reserved |
; +-----------+----------+--------------------------------------------------------------+
; | | | first FAT (all FATs are 1 block) (all entries 3 bytes). |
; | | 0 - 511 | Since blocks 0 - 3 are used for FS info, so file entries |
; | | | start from the 5th entry in the first FAT |
; | +----------+--------------------------------------------------------------+
; | | 0 - 2 | 0x0 (EOF) (1st entry in FAT) |
; | | | (corresponds to 1st block of partition) |
; | +----------+--------------------------------------------------------------+
; | | 3 - 5 | 0x0 (EOF) (2nd entry in FAT) |
; | +----------+--------------------------------------------------------------+
; | | 6 - 8 | 0x0 (EOF) (3rd entry in FAT) |
; | 3 +----------+--------------------------------------------------------------+
; | | 9 - 11 | 0x0 (EOF) (4th entry in FAT) |
; | +----------+--------------------------------------------------------------+
; | | 12 - 14 | 0x6 (topmost dir's next block) (5th FAT entry) |
; | +----------+--------------------------------------------------------------+
; | | 15 - 17 | 0x0 (EOF) (topmost dir's 2nd n last block) (6th FAT entry) |
; | +----------+--------------------------------------------------------------+
; | | 18 - 20 | 0x08 (first block of free blocks list) |
; | +----------+--------------------------------------------------------------+
; | | 21 - 507 | initialised to next block number, |
; | | | part of the free blocks list |
; | +----------+--------------------------------------------------------------+
; | | 508 - 511| block number of next FAT |
; +-----------+----------+--------------------------------------------------------------+
; | 4 | 0 - 511 | reserved |
; +-----------+----------+--------------------------------------------------------------+
; | | | topmost dir's block 1 |
; | 5 | 0 - 511 | (initially only 5th and 6th blocks are allocated) |
; | | | (more blocks are allocated as needed) |
; +-----------+----------+--------------------------------------------------------------+
; | 6 | 0 - 511 | topmost dir's block 2 |
; +-----------+----------+--------------------------------------------------------------+
;
;
; Directory Entry
;
; +--------------+----------------+---------------------+--------------------+-------------------------+-------------------+
; | 16 bytes | 1 byte | 3 bytes | 3 bytes | 6 bytes | 6 bytes |
; +--------------+----------------+---------------------+--------------------+-------------------------+-------------------+
; | File Name | drwx---- | First Block of File | File Size in bytes | Modification Time | Last Access Time |
; | | | | | Year [12] [MSB] | |
; | a-Z, 0-9, | (File Attrbts) | | | Month [4] | |
; | 'dot', | | | | Day [5] | |
; | 'underscore' | | | | Secs in Day [20] [LSB] | |
; +------------- +----------------+---------------------+--------------------+-------------------------+-------------------+
;
;; sign the partition's first sector with XFS Signature and next an optional string
;; about fs creator program, all this within first 64 bytes.
mov ax, word[ds:XFS_SIGNATURE]
mov word[ds:MKFS_BUFFER], ax
mov di, 2
.cploop:
mov al, byte[ds:XFS_string+di-2]
mov byte[ds:MKFS_BUFFER+di], al
inc di
mov ax, word[ds:XFS_string_size]
add ax, 2
cmp di, ax
jnz .cploop
;; fill remaining bytes before byte64 with 0x00
.loop:
cmp di, 64
jz .loop_end
mov byte[ds:MKFS_BUFFER+di], 0x00
inc di
jmp .loop
.loop_end:
;; block num of first FAT at byte 64
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, 0x0003
adc dx, 0
mov word[ds:MKFS_BUFFER+di], ax
mov byte[ds:MKFS_BUFFER+di+2], dl
add di, 3
;; topmost dir's 1st block number
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, 0x0005
adc dx, 0
mov word[ds:MKFS_BUFFER+di], ax
mov byte[ds:MKFS_BUFFER+di+2], dl
add di, 3
;; first block of free blocks list
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, 0x0007
adc dx, 0
mov word[ds:MKFS_BUFFER+di], ax
mov byte[ds:MKFS_BUFFER+di+2], dl
add di, 3
;; last block of free blocks list
mov bx, word[ds:PART_SIZE]
mov cx, word[ds:PART_SIZE+2]
sub bx, 1
sbb cx, 0
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, bx
adc dx, cx
mov word[ds:MKFS_BUFFER+di], ax
mov byte[ds:MKFS_BUFFER+di+2], dl
add di, 3
;; fill remaining bytes before byte512 with 0x00
.loop:
cmp di, 512
jz .loop_end
mov byte[ds:MKFS_BUFFER+di], 0x00
inc di
jmp .loop
.loop_end:
; 1st sector completed, write to disk
mov word[ds:WRITE_OFFSET], 0x0000
mov word[ds:WRITE_OFFSET+2], 0x0000
call WRITE_BUF_TO_SECTOR
;; 2nd sector is reserved, so fill it with zeros
mov di ,0
.loop:
cmp di, 512
jz .loop_end
mov byte[ds:MKFS_BUFFER+di], 0x00
inc di
jmp .loop
.loop_end:
; 2nd sector completed, write to disk
mov word[ds:WRITE_OFFSET], 0x0001
mov word[ds:WRITE_OFFSET+2], 0x0000
call WRITE_BUF_TO_SECTOR
;; 3rd sector is the first FAT
mov di, 0
mov word[ds:MKFS_BUFFER], 0x0000
mov word[ds:MKFS_BUFFER+2], 0x0000
mov word[ds:MKFS_BUFFER+4], 0x0000
mov word[ds:MKFS_BUFFER+6], 0x0000
mov word[ds:MKFS_BUFFER+8], 0x0000
mov word[ds:MKFS_BUFFER+10], 0x0000
add di, 12
mov word[ds:MKFS_BUFFER+di], 0x0006
mov byte[ds:MKFS_BUFFER+di+2], 0x00
add di, 3
mov word[ds:MKFS_BUFFER+di], 0x0000
mov byte[ds:MKFS_BUFFER+di+2], 0x00
add di, 3
; rest of initialisation of the FAT will be done by INIT_FREE_LIST
; 3rd sector completed, write to disk
mov word[ds:WRITE_OFFSET], 0x0002
mov word[ds:WRITE_OFFSET+2], 0x0000
call WRITE_BUF_TO_SECTOR
;; 4th sector reserved, fill with zeros
mov di ,0
.loop:
cmp di, 512
jz .loop_end
mov byte[ds:MKFS_BUFFER+di], 0x00
inc di
jmp .loop
.loop_end:
; 4th sector completed, write to disk
mov word[ds:WRITE_OFFSET], 0x0003
mov word[ds:WRITE_OFFSET+2], 0x0000
call WRITE_BUF_TO_SECTOR
; 5th sector, topmost dir
; for toplevel dir ".." should point to the same dir, like "."
;; first make entry for the doubledot dir file
mov di, 0
.loop:
mov al, byte[ds:ddot_str+di]
mov byte[ds:MKFS_BUFFER+di], al
inc di
cmp di, 16
jnz .loop
mov byte[ds:MKFS_BUFFER+di], 11100000b ; drwx0000
inc di
; it points to the topmost dir itself, since there is no dir above it
; store first block of the dir file
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, 0x0005
adc dx, 0
mov word[ds:MKFS_BUFFER+di], ax
mov byte[ds:MKFS_BUFFER+di+2], dl
add di, 3
; store file size in bytes
mov word[ds:MKFS_BUFFER+di], 0x0400
mov byte[ds:MKFS_BUFFER+di+2], 0x00
add di, 3
; ignore mod time and access time for now, to be implemented later
.loop:
mov byte[ds:MKFS_BUFFER+di], 0x00
inc di
cmp di, 35
jnz .loop
;; 2nd entry is for the dot dir file
mov di, 0
.loop:
mov al, byte[ds:dot_str+di]
mov byte[ds:MKFS_BUFFER+35+di], al
inc di
cmp di, 16
jnz .loop
mov byte[ds:MKFS_BUFFER+35+di], 11100000b ; drwx0000
inc di
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, 0x0005
adc dx, 0
mov word[ds:MKFS_BUFFER+35+di], ax
mov byte[ds:MKFS_BUFFER+35+di+2], dl
add di, 3
mov word[ds:MKFS_BUFFER+35+di], 0x0400
mov byte[ds:MKFS_BUFFER+35+di+2], 0x00
add di, 3
.loop:
mov byte[ds:MKFS_BUFFER+35+di], 0x00 ; right now we leave the modify and access times
inc di
cmp di, 35
jnz .loop
;; now we must make an entry to indicate last entry in the dir, for this we use the fact
;; that filename can't start with null bytes. So we need to check only the first two bytes
;; to verify the END OF ENTRIES marker. Its not necessary to fill null bytes after the
;; marker but we still do it
mov di, 70 ; since we have made two entries till now
.loop:
mov byte[ds:MKFS_BUFFER+di], 0x00 ; right now we leave the modify and access times
inc di
cmp di, 512
jnz .loop
;; finally write the 5th block to disk
mov word[ds:WRITE_OFFSET], 0x0004
mov word[ds:WRITE_OFFSET+2], 0x0000
call WRITE_BUF_TO_SECTOR
;; Although not necessary, we fill the topmost dir file's 2nd block with nulls just to ensure cleanliness
mov di, 0
.loop:
mov byte[ds:MKFS_BUFFER+di], 0x00 ; right now we leave the modify and access times
inc di
cmp di, 512
jnz .loop
;; finally write the 6th block to disk
mov word[ds:WRITE_OFFSET], 0x0005
mov word[ds:WRITE_OFFSET+2], 0x0000
call WRITE_BUF_TO_SECTOR
AllocateFATs:
mov ax, word[ds:PART_SIZE]
sub ax, 170
push ax
mov ax, word[ds:PART_SIZE+2]
sbb ax, 0
push ax
.loop:
pop ax
cmp ax, 0
jz .check_low_bits
.check_low_bits:
InitFreeBlocks:
;; Now its time to initialise the free blocks list
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, 0x0007
adc dx, 0
mov di, 0
WRITE_BUF_TO_SECTOR:
; write sector to disk
mov ax, word[ds:START_SECTOR]
mov dx, word[ds:START_SECTOR+2]
add ax, word[ds:WRITE_OFFSET]
adc dx, word[ds:WRITE_OFFSET+2]
call GET_CHS
push word MKFS_BUFFER
push word ds
push word[PART_START_SECTOR]
push word[PART_START_HEAD]
push word[PART_START_CYL]
push word 0
push word[MKFS_BUFFER_SIZE]
mov bx, ss
mov es, bx
mov bx, sp
mov ax, 1
int 0x26
add sp, 14
ret
GET_CHS:
; get CHS of Start Sector, dx:ax contains the sector number
sub ax, 1
sbb dx, 0
mov bx, 16128
div bx
mov word[ds:PART_START_CYL], ax
mov ax, dx
mov dx, 0
mov bx, 63
div bx
mov word[ds:PART_START_HEAD], ax
inc dx
mov word[ds:PART_START_SECTOR], dx
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;
ask_start_sector_str DB "first sector of the partition to be formatted with XFS : "
ask_start_sector_str_size DW ($-ask_start_sector_str)
newline_str DB 0x0D, 0x0A
newline_str_size DW ($-newline_str)
ask_part_size_str DB "size of the partition : "
ask_part_size_str_size DW ($-ask_part_size_str)
invalid_value_str DB "invalid value for "
invalid_value_str_size DW ($-invalid_value_str)
allowed_range_str DB 0x0D, 0x0A, "allowed range "
allowed_range_str_size DW ($-allowed_range_str)
size_overflow_msg DB "Size Overflow.", 0x0D, 0x0A
size_overflow_msg_size DW ($-size_overflow_msg)
dot_str DB ".",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
ddot_str DB "..",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
sc_code DB 0x00
char_code DB 0x00
START_SECTOR DD 0x00000000
PART_SIZE DD 0x00000000
WRITE_OFFSET DD 0x00000000
PART_START_CYL DW 0x0000
PART_START_HEAD DW 0x0000
PART_START_SECTOR DW 0x0000
XFS_SIGNATURE DW 0x66BB
XFS_string DB "XFS FILESYSTEM CREATED BY MKXFS V0.1",0x0D, 0x0A
XFS_string_size DW ($-XFS_string)
MKFS_BUFFER TIMES 512 DB 0x00
MKFS_BUFFER_SIZE DW 512
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ed: a simple text editor
;;
ed:
pusha
getChar:
xor ax, ax
mov ah, 0x10
int 0x16
mov byte[scode], ah
mov byte[char], al
cmp byte[scode], 0x01 ; look for ESC keypress
jz WRITE_BUFFER_TO_DISK
cmp byte[char], 32
jl getChar
cmp byte[char], 126
jg getChar
mov si, word char
xor cx, cx
mov cl, 1
call printString
mov di, word[BUFFER_OFFSET]
mov byte[ds:ED_BUFFER+di], al
inc word[BUFFER_OFFSET]
inc di
cmp di, word[BUFFER_SIZE]
jz WRITE_BUFFER_TO_DISK
jmp getChar
WRITE_BUFFER_TO_DISK:
mov si, word write_msg
xor cx, cx
mov cl, byte[write_msg_size]
call printString
push word ED_BUFFER
push word ds
push word 20
push word 0
push word 0
push word[BYTES_WRITTEN_TO_DISK]
push word[BUFFER_OFFSET]
mov bx, ss
mov es, bx
mov bx, sp
mov ax, 1
int 0x26
add sp, 14
mov si, word write_done
xor cx, cx
mov cl, byte[write_done_size]
call printString
cmp byte[scode], 0x01
jz BYE
mov ax, word[BUFFER_OFFSET]
add word[BYTES_WRITTEN_TO_DISK], ax
mov word[BUFFER_OFFSET], 0
jmp getChar
BYE:
mov si, word bye_msg
xor cx, cx
mov cl, byte[bye_msg_size]
call printString
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
write_msg DB 0x0d, 0x0a, "going to write buffer to disk..."
write_msg_size DB ($-write_msg)
write_done DB 0x0d, 0x0a, "write successful."
write_done_size DB ($-write_done)
bye_msg DB 0x0d, 0x0a, "Leaving editor....", 0x0d, 0x0a
bye_msg_size DB ($-bye_msg)
scode DB 0x00
char DB 0x00
ED_BUFFER TIMES 512 DB 0x00
BUFFER_SIZE DW 512
BUFFER_OFFSET DW 0x0000
BYTES_WRITTEN_TO_DISK DW 0x0000
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; printString: PRINT MESSAGES ON THE TERMINAL
;;
printString:
pusha
push si
push word ds
push cx
mov bx, ds
mov es, bx
mov bx, sp
mov ax, 4
int 0x26
add sp, 6
;; save current cursor position
mov ah, 0x03
mov bh, 0x00
int 0x10
mov [CURR_CURSOR_ROW], dh
mov [CURR_CURSOR_COL], dl
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Section
;;
__PROC_LOAD_SEGMENT_NUM__ DW 0x0000
CURR_CURSOR_ROW DB 0X0000
CURR_CURSOR_COL DB 0X0000
SPACE_STRING DB " "
SCAN_STRING DB "SCANCODE:0x"
SCAN_STRING_SIZE DB 11
ASCII_STRING DB 0xD, 0xA,"ASCII:"