elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  ASM (Moderador: Eternal Idol)
| | | |-+  int 13h problema
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: int 13h problema  (Leído 3,680 veces)
Khronos14


Desconectado Desconectado

Mensajes: 443


A lie is a lie


Ver Perfil WWW
int 13h problema
« en: 21 Julio 2012, 15:40 pm »

Hola, estoy escribiendo mi propio bootloader, que se carga desde una imagen FAT12 de disquete. Utilizo bochs para la virtualización.

El problema que tengo, es que no se como trabajar con el buffer que devuelve la función int 13h cuando lees unos sectores del disquete.

Código
  1. [bits 16]
  2. [ORG 0]
  3.  
  4. jmp short start
  5. nop   ; No Operation (1 byte)
  6.  
  7. OEMLabel:                     db "KERNEL  "      ; 8 characters padded with spaces
  8. BytesPerSector:                 dw 512            ; Bytes per sector
  9. SectorsPerCluster:                db 1            ; Sectors per cluster
  10. ReservedSectors:                dw 1            ; Reserved Sectors (for sector 0)
  11. NumberOfFATs:                   db 2            ; Number of FAT´s
  12. MaxRootEntries:                dw 224            ; Number of Root Entries
  13.  
  14. NumberOfSectors:                dw 2880            ; Number of sectors
  15. DeviceDescriptor:                db 0xF0            ; Device Descriptor 0xF0 => 1.44 MB floppy image
  16. SectorsPerFAT:                   dw 9            ; Sectors Per FAT
  17. SectorsPerTrack:                dw 18            ; Sectors Per Track
  18. Sides:                         dw 2            ; Sides of disk
  19. HiddenSectors:                   dd 0            ; Number of Hidden Sectors
  20. LengthOfSectors:               dd 0            ; Length of sectors
  21. DriveNo:                      db 0            ; Drive Number (0 or 1)
  22. Flags:                         db 0            ; Additional flags
  23. Signature:                      db 0x14            ; Signature, some number of 1 byte
  24. VolumeID:                     dd 0xAABBCCDD      ; Volume ID
  25. VolumeLabel:                   db "DISCO TANIS "   ; 11 characters padded with spaces
  26. FileSystem:                   db "FAT12   "      ; 8 characters padded with spaces
  27.  
  28. ;**********************************************************;
  29. ; Entry Point
  30. ; Reset the floppy disk.
  31. ; Calculate the root directory CHS address and jump to
  32. ; read_root_directory.
  33. ;**********************************************************;
  34. start:
  35.   jmp 07C0h:stage_1
  36.  
  37. stage_1:
  38.   mov ax, cs
  39.   mov ds, ax
  40.   mov es, ax
  41.  
  42.   mov si, StringMsg
  43.   call print_string
  44.  
  45.   xor ah, ah ; Ah = 0, reset function
  46.   mov dl, BYTE [DriveNo]
  47.   int 13h ; Reset Floppy Disk
  48.  
  49.   xor ax, ax
  50.  
  51.   add ax, WORD [SectorsPerFAT]
  52.   mul BYTE [NumberOfFATs]
  53.   add ax, WORD [ReservedSectors]
  54.   ; AX = (SectorsPerFAT * NumberOfFATs) + ReservedSectors
  55.  
  56.   call lba2chs
  57.  
  58.   jmp short read_root_directory
  59.  
  60. read_root_directory:
  61.   ; We have already calculated the CH = Cilynder, CL = sector and
  62.   ; DH = Head.
  63.   mov ax, 1000h
  64.   mov es, ax
  65.   mov bx, 0
  66.  
  67.   mov ah, 02h   ; Read mode
  68.   mov al, 0Fh ; Sectors to read (512 bytes each sector)
  69.   mov dl, BYTE [DriveNo]
  70.  
  71.   int 13h ;Call the interruption!
  72.   jc .root_dir_read_error
  73.  
  74.   mov si, [1000h]
  75.   mov dx, 512
  76.   call print_n
  77.  
  78.        ...
  79.  

Y la función print_n

Código
  1. ;**********************************************************;
  2. ; Print a string in screen
  3. ; SI => String pointer
  4. ; DX => Number of characters to print
  5. ;**********************************************************;
  6. print_n:
  7.   push ax
  8.   push bx
  9.   push cx
  10.  
  11.   mov ah, 0Eh
  12.  
  13.   xor cx, cx
  14.  
  15.   .loop:
  16.   mov al, [si]
  17.   int 10h
  18.   inc cx
  19.   cmp cx, dx
  20.   je short .end_loop
  21.  
  22.   inc si
  23.   jmp short .loop
  24.  
  25.   .end_loop:
  26.   pop ax
  27.   pop bx
  28.   pop cx
  29.   ret
  30.  

El problema que tengo, está aquí:

Código
  1. mov si, [1000h]
  2. mov dx, 512
  3. call print_n
  4.  

Me debería escribir en pantalla en caracteres ASCII el contenido del Root Directory, pero en vez de eso, comienza a escribirme desde el sector 0, es decir, va escribiendo la propia memoria del programa...
La función lba2chs, convierte direccionamiento LBA (Logical Block Address) a CHS (Cylinder Head Sector), y funciona bien.

Espero que me podáis ayudar.

Saludos.


En línea

_Enko


Desconectado Desconectado

Mensajes: 538



Ver Perfil WWW
Re: int 13h problema
« Respuesta #1 en: 21 Julio 2012, 16:53 pm »

No recuerdo bien el tema, pero igual, creo que deberias de poner el codigo completo...

la funcion print_n se encuentra en el loader o esta dentro de lo que se lee del disquet es decir, el kernel?

Si esta fuera del loader, primero tienes que hacer un JMP hacia la direccion de memoria donde se almacenó lo que se leyo del disquet. Y ahi tiene que haber un ORG para que los saltos relativos funcionen.

en PRINT_N los pop estan mal.
Se saca de la pila en el orden inverso al que se meten. En cambio vos lo haces con el mismo orden, alternando ax con cx.



por cierto, si no te quieres quemar la cabeza momentaneamente con fat12/16 no lo tienes que hacer. No es obligatorio.
Al menos que uses virtual box de oracle que no acepta un disquete sin formato.
WMWare por ejemplo, acepta disquet sin formato e inclusive con tamaño de cualquier tipo. Por ejemplo disquet de 5megas (inexistente) te lo acepta.

Lo que puedes hacer es por ejemplo, en tu codigo:


org 0;
lectura del disquete a partir de 512bytes en adelante... no se, lees 2kb, con eso sobra
salto a la direccion de memoria a  donde leiste el kernel.
rellenar este pedazo que ocupe 512bytes.

;kernel CODE
org (direccion de memoria de a donde leiste los 2kb)
;la direccion en el disquete es el byte 513... pero no es donde leiste eso en la memoria
;por eso el org por ejemplo 1000h

codigo del kernel

rellenas para que ocupe todo 1.44M


es decir, el codigo del kernel no se ejecuta desde el disuqet, sino desde la memoria a lo que lo pasaste.


« Última modificación: 21 Julio 2012, 17:05 pm por _Enko » En línea

Khronos14


Desconectado Desconectado

Mensajes: 443


A lie is a lie


Ver Perfil WWW
Re: int 13h problema
« Respuesta #2 en: 21 Julio 2012, 21:10 pm »

Muchas gracias por responder _Enko, entonces ¿podría hacer algo así?:

Código
  1. mov ax, 1000h
  2.   mov es, ax
  3.   mov bx, 0
  4.  
  5.   mov ah, 02h   ; Read mode
  6.   mov al, 0Fh ; Sectors to read (512 bytes each sector)
  7.   mov dl, BYTE [DriveNo]
  8.  
  9.   int 13h ;Call the interruption!
  10.   jc .root_dir_read_error
  11.  
  12.   jmp 1000h:0000
  13.   xor bx, bx
  14.   mov si, bx [b]?[/b]
  15.   mov dx, 512
  16.   call print_n
  17.  

Print_n se encuentra en el loader, puse los puntos suspensivos porque a continuación está la búsqueda en el Root Directory, la búsqueda en la File Allocation Table, etc..

Lo de los push, fue un pequeño error, gracias.

Lo de la imagen FAT, ya lo se. Pero lo prefiero hacer así, porque así podría cargar mis propios programas una vez que el kernel esté más avanzado. Leer y buscar archivos en FAT12, ya lo tengo listo.

Voy a probar a hacer lo que me dices y luego te comento.

Gracias, saludos.
En línea

_Enko


Desconectado Desconectado

Mensajes: 538



Ver Perfil WWW
Re: int 13h problema
« Respuesta #3 en: 21 Julio 2012, 21:37 pm »

Citar
jmp 1000h:0000
   xor bx, bx
   mov si, bx ?
   mov dx, 512
   call print_n
Eso no... por lo menos no asi.
Todo el codigo que se encuentra despues de JMP no se va ejecutar.

algo asi seria parox, no lo tomes literal
Código:
org 0
....
....
;loader
...
...
;kernel jump
jmp 1000h:0000
db  512 - $ dup  0 ;rellenamos con 512 bytes.
;kernel
org 1000h?
;to do

En línea

Khronos14


Desconectado Desconectado

Mensajes: 443


A lie is a lie


Ver Perfil WWW
Re: int 13h problema
« Respuesta #4 en: 25 Julio 2012, 16:33 pm »

Bueno, después de una dura semana programando he conseguido terminar el Stage_1 de mi bootloader. No fue una tarea fácil, pero valió la pena, además aprendí muchísimo. El Bootloader busca en una imagen FAT12 el archivo STAGE_2.BIN y lo carga en memoria.

Podeis crear una imagen FAT12 con herramientas como esta:

https://sourceforge.net/projects/fat12maker/

También desarrollada por mi, totalmente escrita en C.

Código
  1. ; Developed by Khronos
  2.  
  3. [bits 16] ; Real mode
  4.  
  5. [ORG 0] ; Origin address
  6.  
  7. jmp short start ; 2 bytes
  8. nop ; No Operation (1 byte)
  9.  
  10. OEMLabel: db "KERNEL  " ; 8 characters padded with spaces
  11. BytesPerSector:   dw 512 ; Bytes per sector
  12. SectorsPerCluster: db 1 ; Sectors per cluster
  13. ReservedSectors: dw 1 ; Reserved Sectors (for sector 0)
  14. NumberOfFATs: db 2 ; Number of FAT´s
  15. MaxRootEntries: dw 224 ; Number of Root Entries
  16.  
  17. NumberOfSectors: dw 2880 ; Number of sectors
  18. DeviceDescriptor: db 0xF0 ; Device Descriptor 0xF0 => 1.44 MB floppy image
  19. SectorsPerFAT: dw 9 ; Sectors Per FAT
  20. SectorsPerTrack: dw 18 ; Sectors Per Track
  21. Sides: dw 2 ; Sides of disk
  22. HiddenSectors: dd 0 ; Number of Hidden Sectors
  23. LengthOfSectors: dd 0 ; Length of sectors
  24. DriveNo: db 0 ; Drive Number (0 or 1)
  25. Flags: db 0 ; Additional flags
  26. Signature: db 0x14 ; Signature, some number of 1 byte
  27. VolumeID: dd 0xAABBCCDD ; Volume ID
  28. VolumeLabel: db "DISCO TANIS " ; 11 characters padded with spaces
  29. FileSystem: db "FAT12   " ; 8 characters padded with spaces
  30.  
  31.  
  32. start:
  33. jmp 07C0h:stage_1 ; CS = 07C0h
  34.  
  35. ;*******************************************************************
  36. ;* STAGE 1
  37. ;*******************************************************************
  38. stage_1:
  39. mov ax, cs
  40. mov es, ax ; ES = 07C0h
  41. mov ds, ax ; DS = 07C0h
  42.  
  43. mov si, BootMsg
  44. call print_string ; Print Boot Message
  45.  
  46. xor ah, ah ; Ah = 0, reset function
  47. xor dl, dl ; DL = 0, Drive number 0
  48. int 13h ; Reset Floppy Disk
  49.  
  50. jnc short read_root_directory ; If no errors, jump read_root_directory
  51.  
  52. jmp reboot ; else, reboot!
  53.  
  54.  
  55. ; Root Directory starts at LBA 19 with
  56. ; 15 sectors of size
  57. read_root_directory:
  58. mov ax, 1000h
  59.    mov es, ax ; ES = 1000h
  60.  
  61. ;Vamos a leer el sector número 1
  62. mov ax, 19 ; 19 = Root Directory
  63. call lba2chs ; LBA to CHS
  64.  
  65. mov ah, 02h ; Read function
  66. mov al, 0Fh ; Sectors to read
  67. xor bx, bx ; ES:BX = 1000h:0000
  68. xor dl, dl ; DL = 0, Drive 0
  69.  
  70. int 13h ;Read!
  71.  
  72. jnc short find_file ; If no errors, jump find_file
  73.  
  74. jmp reboot ; else, reboot!
  75.  
  76. find_file:
  77. mov ax, 1000h
  78. mov ds, ax ; DS = 1000h
  79.  
  80. mov bx, 4640 ; bx = (0Fh * 512) + 32
  81. mov cx, -32
  82. .loop:
  83. mov dl, 1 ; DL = FALSE
  84.  
  85. sub bx, 32 ; BX - 32
  86. or bx, bx
  87. jz .endloop ; If BX = 0 then, jump .endloop
  88.  
  89. add cx, 32 ; CX += 32
  90. mov si, cx
  91.  
  92. lodsb ; mov al, [SI]; inc si
  93. cmp al, 'S'
  94. jne short .loop
  95.  
  96. lodsb
  97. cmp al, 'T'
  98. jne short .loop
  99.  
  100. lodsb
  101. cmp al, 'A'
  102. jne short .loop
  103.  
  104. lodsb
  105. cmp al, 'G'
  106. jne short .loop
  107.  
  108. lodsb
  109. cmp al, 'E'
  110. jne short .loop
  111.  
  112. lodsb
  113. cmp al, '_'
  114. jne short .loop
  115.  
  116. lodsb
  117. cmp al, '2'
  118. jne short .loop
  119.  
  120. lodsb
  121. cmp al, ' '
  122. jne short .loop
  123.  
  124. lodsb
  125. cmp al, 'B'
  126. jne short .loop
  127.  
  128. lodsb
  129. cmp al, 'I'
  130. jne short .loop
  131.  
  132. lodsb
  133. cmp al, 'N'
  134. jne short .loop
  135.  
  136. xor dl, dl ; DL = TRUE
  137. add si, 0Fh ; SI current Pos + 15
  138. mov cx, [si] ; Get the Starting Cluster
  139.  
  140. .endloop:
  141.  
  142. or dl, dl
  143. jz short read_fat ; If DL = TRUE then, jump read_fat
  144.  
  145. jmp reboot ; else, reboot!
  146.  
  147. read_fat:
  148. ; We will overwrite the Root Directory with the FAT
  149. mov ax, 1000h
  150. mov es, ax ; ES = 1000h
  151.  
  152. mov ax, 1 ; LBA = 1
  153. push cx ; We save the Starting Cluster
  154.  
  155. call lba2chs
  156.  
  157. mov ah, 02h ; Function 02h: Read
  158. mov al, 9 ; Sectors to read = SectorsPerFAT
  159. xor dl, dl ; Drive = 0
  160. xor bx, bx ; 1000h:0000
  161.  
  162. int 13h
  163.  
  164. jnc short read_data ; if no errors, then jump read_data
  165.  
  166. jmp reboot ; else, reboot!
  167.  
  168. read_data:
  169. ; FAT cluster 0 = media descriptor = 0F0h
  170. ; FAT cluster 1 = file cluster = 0FFh
  171. ; Cluster start = ((cluster number) - 2) * SectorsPerCluster + (start of user)
  172. ;               = (cluster number) + 31
  173.  
  174. pop cx ; Restore the Starting Cluster
  175.  
  176. xor bx, bx ; BX = 0
  177. push bx ; Push!
  178.  
  179. mov bx, 3000h
  180. mov es, bx ; ES:BX = 3000h:0000
  181. ; Segment where we will load the stage_2
  182. .loop:
  183. mov si, 0000h ; SI Pointer to 1000h:0000
  184.  
  185. mov ax, cx ; AX = CX
  186. mov bx, 2 ; BX = 2
  187. div bl ; AH = AX MOD BL
  188.  
  189. or ah, ah
  190. jz short .par ; If remainder = 0 then, jump .par
  191. .impar: ; d = (n - 1 / 2) * 3 + 1
  192. mov ax, cx ; AX = CX
  193. dec ax
  194. shr ax, 1
  195. mov bx, 3 ; BX = 3
  196. mul bx ; AX * 3
  197. inc ax
  198.  
  199. add si, ax ; SI + AX
  200. mov dx, [si] ; We get the 16 bits of FAT, but we only need 12 bits
  201.  
  202. shr dx, 4 ; DX >> 4
  203. jmp short .next
  204.  
  205. .par: ; d = (n / 2) * 3
  206. mov ax, cx ; AX = CX
  207. shr ax, 1
  208. mov bx, 3 ; BX = 3
  209. mul bx ; AX * 3
  210.  
  211. add si, ax ; SI + AX
  212. mov dx, [si] ; We get the 16 bits of FAT, but we only need 12 bits
  213.  
  214. and dx, 0x0FFF ; DX AND 0FFFh
  215. .next:
  216.  
  217. mov ax, 31 ; AX = 31
  218. add ax, cx ; AX = AX + CX (Starting or next cluster)
  219.  
  220. pop bx ; Restore BX
  221.  
  222. push dx ; Save next cluster
  223. call lba2chs
  224. mov ah, 02h ; Read function
  225. mov al, 1 ; 1 sector to read
  226. xor dl, dl ; Drive = 0
  227.  
  228. int 13h
  229.  
  230. pop dx ; Restore next cluster
  231.  
  232. cmp dx, 0x0FF8 ; Compare Next Cluster with 0x0FF8
  233. jae short execute ; if dx >= 0x0FFF8 then, jump execute
  234.  
  235. add bx, 512 ; BX = BX + 512
  236. push bx ; Save BX
  237.  
  238. mov cx, dx
  239. jmp short .loop
  240.  
  241. execute:
  242. jmp 3000h:0000
  243.  
  244.  
  245.  
  246. ;**********************************************************;
  247. ; Print a string in screen
  248. ; SI => String pointer with NULL terminator
  249. ;**********************************************************;
  250. print_string:
  251. pusha
  252. mov ah, 0Eh
  253.  
  254. .loop:
  255. mov al, [si]
  256. or al, al
  257. jz short .end_loop
  258. int 10h
  259. inc si
  260. jmp short .loop
  261.  
  262. .end_loop:
  263.  
  264. mov al, 13
  265. int 10h
  266. mov al, 10
  267. int 10h
  268.  
  269. popa
  270. ret
  271.  
  272. ;**********************************************************;
  273. ; Convert LBA address to CHS address
  274. ; AX => LBA address
  275. ;
  276. ; CH <= Cilynder
  277. ; DH <= Head
  278. ; CL <= Sector
  279. ;**********************************************************;
  280. lba2chs:
  281. push bx ; Push bx
  282. mov bx, ax ; Copy LBA in BX
  283.  
  284. ;AX has the LBA value
  285. mov dl, 18 ; BL = Sectors Per Track
  286. div dl
  287. xchg ax, bx
  288. ;BL = Temp = LBA / (Sectors Per Track)
  289. ;BH = LBA % (Sectors Per Track)
  290. mov cl, bh
  291. inc cl ; CL = Sector = [LBA % (Sectors Per Track)] + 1
  292.  
  293. xor ax, ax ; AX = 0
  294. mov al, bl ; AL = Temp = LBA / (Sectors Per Track)
  295. mov dx, 2
  296. div dl
  297. ;AL = LBA / (Number of Heads)
  298. ;AH = LBA % (Number of Heads)
  299. mov dh, ah ; Head
  300. mov ch, al ; Cylinder
  301.  
  302. pop bx
  303. ret
  304.  
  305.  
  306. ;**********************************************************;
  307. ; Print a error message, then wait for a keypress and reboot
  308. ;**********************************************************;
  309. reboot:
  310. mov ax, 07C0h
  311. mov ds, ax ; DS = 07C0h
  312.  
  313. mov si, Error
  314. call print_string
  315.  
  316. mov si, Reboot
  317. call print_string
  318.  
  319. mov ax, 0
  320. int 16h ; Wait for keystroke
  321. mov ax, 0
  322. int 19h ; Reboot the system
  323.  
  324.  
  325. ;**********************************************************;
  326. ; Variables
  327. ;**********************************************************;
  328. BootMsg: db "Loading...", 0
  329.  
  330. Error: db "Missing or corrupt STAGE_2.BIN!", 0
  331. Reboot: db "Press any key to reboot...", 0
  332.  
  333.  
  334. ;**********************************************************;
  335. ; EOF
  336. ;**********************************************************;
  337.  
  338. times 510-($-$$) db 0 ; Fill with zero´s
  339. dw 0xAA55 ; Bootloader signature
  340.  

Y aquí esta el stage_2.asm

Código
  1. ;*******************************************************************
  2. ;* STAGE 2
  3. ;*******************************************************************
  4.  
  5. [bits 16]
  6. [ORG 0]
  7.  
  8. stage2:
  9. mov ax, 3000h
  10. mov ds, ax
  11. mov es, ax
  12. mov fs, ax
  13. mov gs, ax
  14.  
  15. mov si, HelloString
  16. call print_string2
  17. jmp test
  18.  
  19.  
  20. print_string2:
  21. pusha
  22. mov ah, 0Eh
  23.  
  24. .loop:
  25. mov al, [si]
  26. or al, al
  27. jz short .end_loop
  28. int 10h
  29. inc si
  30. jmp short .loop
  31.  
  32. .end_loop:
  33.  
  34. mov al, 13
  35. int 10h
  36. mov al, 10
  37. int 10h
  38.  
  39. popa
  40. ret
  41.  
  42. times 512 db 'A'
  43. times 512 db 'B'
  44.  
  45. test:
  46. mov si, TestString
  47. call print_string2
  48. jmp $
  49.  
  50. times 512 db 'C'
  51. times 2048 db 'D'
  52.  
  53. HelloString: db "Hello from Stage_2!", 0
  54. TestString: db "This is a test", 0
  55.  

Para crear la imagen:
fat12maker -b Stage_1.bin -i Stage_2.bin About.txt .... -o Kernel.img

Ahora voy a trabajar en el Kernel, tengo que activar el modo largo y empezar a programar en C.

Si encontráis errores o tenéis alguna sugerencia, postearlo aquí.

Saludos y gracias.
« Última modificación: 25 Julio 2012, 16:38 pm por Khronos14 » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Problema BlueZScanner y problema de conexión
Hacking Mobile
Kasswed 3 6,297 Último mensaje 6 Mayo 2006, 22:04 pm
por Gospel
Problema Css o problema con el sidebar
Desarrollo Web
gaboomsk8 6 3,565 Último mensaje 27 Diciembre 2011, 21:56 pm
por gaboomsk8
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines