ext2 read Block Group Descriptor

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
onlshk
Posts: 9
Joined: Wed Sep 24, 2014 2:42 am

ext2 read Block Group Descriptor

Post by onlshk »

Hello,

i have a disk which created with:

Code: Select all

	dd if=/dev/zero of=zerOS.img bs=1048576 count=10
	(echo "n"; echo "p"; echo ""; echo ""; echo ""; echo "w") | sudo fdisk -u -C10 -S63 -H16 zerOS.img

	sudo rm -rf /mnt
	sudo mkdir -p /mnt/fcd

	sudo losetup /dev/loop0 zerOS.img
	sudo mke2fs -b1024 /dev/loop0
	sudo mount -t ext2 /dev/loop0 /mnt/fcd

	sudo dd if=loader.bin of=zerOS.img count=1 conv=notrunc
	sudo dd if=stage1.bin of=zerOS.img count=1 conv=notrunc seek=1

	sudo cp kernel.bin /mnt/fcd

	sleep 0.1
	sudo umount /mnt/fcd
	sudo losetup -d /dev/loop0
I copied bootloader and kernel.bin there. Now i want to load kernel.bin. But before i must to find it's inode. First of all i need to load 2 inode for root directory. Trying to load block group descriptor from block group descriptor table, it is: 2 sectors (al), 0 cylinder (ch), 5 sector (cl), 0 head (dh) and buffer bx. After i read it, i get 'Number of directories in group' from this descriptor with

Code: Select all

mov ax, [buffer + 16]
, but everytime i'm getting 2 in ax, event i put 5 files/dirs there.

And i can't undersratnd, why it everytime shows 2. or i built image in wrong way or i'm reading it wrong way.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: ext2 read Block Group Descriptor

Post by Combuster »

Code: Select all

# create partition table with one partition
   (echo "n"; echo "p"; echo ""; echo ""; echo ""; echo "w") | sudo fdisk -u -C10 -S63 -H16 zerOS.img
# use entire disk
   sudo losetup /dev/loop0 zerOS.img
# destroy the partition table and use the entire disk as ext2
   sudo mke2fs -b1024 /dev/loop0

# ...
# profit
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
onlshk
Posts: 9
Joined: Wed Sep 24, 2014 2:42 am

Re: ext2 read Block Group Descriptor

Post by onlshk »

Combuster wrote:

Code: Select all

# create partition table with one partition
   (echo "n"; echo "p"; echo ""; echo ""; echo ""; echo "w") | sudo fdisk -u -C10 -S63 -H16 zerOS.img
# use entire disk
   sudo losetup /dev/loop0 zerOS.img
# destroy the partition table and use the entire disk as ext2
   sudo mke2fs -b1024 /dev/loop0

# ...
# profit
hmm... So how can i make this in right way?
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: ext2 read Block Group Descriptor

Post by jnc100 »

In addition to what Combuster posted above, you can quite easily tell whether its a problem with creating the image or reading the image in your OS by examining the image file with a hex editor and seeing if what you expect to be in a certain place is actually there.

Furthermore, I'd recommend against having your ext2 driver work with CHS addressing - have it use LBA instead and then either use the extended read BIOS functions or convert to CHS somewhere in your block layer.

Regards,
John.
onlshk
Posts: 9
Joined: Wed Sep 24, 2014 2:42 am

Re: ext2 read Block Group Descriptor

Post by onlshk »

# create partition table with one partition
(echo "n"; echo "p"; echo ""; echo ""; echo ""; echo "w") | sudo fdisk -u -C10 -S63 -H16 zerOS.img

Am i right that this partition will start from 2048?

I created partition and write bootloader to disk with:

Code: Select all

dd if=/dev/zero of=zerOS.img bs=1048576 count=10
sudo losetup /dev/loop0 zerOS.img
(echo "n"; echo "p"; echo ""; echo ""; echo ""; echo "w") | sudo fdisk -u -C10 -S63 -H16 zerOS.img
sudo fdisk -u -C10 -S63 -H16 zerOS.img
# write bootloader
sudo dd if=loader.bin of=zerOS.img count=1 bs=512 conv=notrunc
sudo dd if=stage1.bin of=zerOS.img count=1 bs=512 conv=notrunc seek=1
sudo losetup -d /dev/loop0
Now i set up another loop device and mounted ext2 with offset:

Code: Select all

sudo losetup -o 1048576 /dev/loop1 zerOS.img

# 1024 block size
sudo mke2fs -b1024 /dev/loop1
sudo mount -t ext2 /dev/loop1 /mnt/fcd

# copy kernel
sudo cp kernel.bin /mnt/fcd

sudo umount /mnt/fcd
sudo losetup -d /dev/loop1
Now bootloader works, but now i can't read superblock. What's wrong here?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: ext2 read Block Group Descriptor

Post by Combuster »

Which superblocks were you unable to find? The Ext2 one or the partition table one?

I also wouldn't normally trust a power of two in partition table offsets.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
onlshk
Posts: 9
Joined: Wed Sep 24, 2014 2:42 am

Re: ext2 read Block Group Descriptor

Post by onlshk »

previously i read superblock at sector 3 with:

Code: Select all

mov ah, 0x02
mov al, 1
mov cx, 0x0003
xor dh, dh
mov bx, buffer
int 0x13
Now i can't understand where to read it.
Post Reply