;================================================================================= ; BLKGETSIZE demonstration. ; ; Retrieves size of a block device. ; usage: ; $ ./devinfo /dev/hda ; ; The LSCR Project. ;================================================================================= format ELF include '../macros.inc' include '../../include/symbols.inc' include '../../include/structs.inc' extrn error_chk section '.text' executable public _start _start: pop eax ; retrieve command-line arguments number cmp eax, 2 jne usage pop eax pop ebx mov eax, SYS_OPEN ; open the device mov ecx, O_RDONLY mov edx, S_IRUSR int 0x80 ccall error_chk, <"SYS_OPEN has failed: ">, exit mov [file_d], eax mov eax, SYS_IOCTL ; get physical block size mov ebx, [file_d] mov ecx, BLKSSZGET mov edx, blksize int 0x80 ccall error_chk, <"BLKSSZGET has failed: ">, close mov eax, SYS_IOCTL ; get dev size in block mov ebx, [file_d] mov ecx, BLKGETSIZE mov edx, devsize int 0x80 ccall error_chk, <"BLKGETSIZE has failed: ">, close finit ; convert the size to a printable form e.g. "74.53 GB" fild [devsize] ; formula for getting size in GB: devsize*blocksize/(1024^3) fimul [blksize] mov [tmp], 1024*1024*1024 fidiv [tmp] fldlg2 fld st1 fabs fyl2x fstcw [cw] or [cw], 0000110000000000b fldcw [cw] fistp [expn] mov [tmp], 100 fimul [tmp] fbstp [bcd] mov esi, bcd+9 ; convert BCD to ASCII mov edi, ascii mov ecx, 9 @@: dec esi movzx eax, byte [esi] ror ax, 4 ror ah, 4 add ax, 3030h stosw dec ecx jnz @b inc ecx mov eax, [expn] or eax, eax ; negative size? js @f add ecx, eax @@: mov esi, ascii sub esi, ecx add esi, 16 mov edi, size_s+20 rep movsb mov al, "." mov ecx, 2 stosb rep movsb mov eax, dword 0x0a424720 ; " GB",0xa stosd sub edi, size_s mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, size_s mov edx, edi int 0x80 close: mov eax, SYS_CLOSE mov ebx, [file_d] int 0x80 exit: mov eax, SYS_EXIT xor ebx, ebx int 0x80 usage: mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, usage_s mov edx, usage_s_sz int 0x80 jmp exit section '.data' writeable usage_s db "USAGE: ./devinfo ",0xa usage_s_sz = $-usage_s file_d rd 1 blksize rd 1 devsize rd 1 size_s db "Size of the device: " rb 10 bcd rt 1 ascii rb 20 expn rd 1 cw rw 1 tmp rd 1