Page 1 of 3

How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 11:57 am
by Caffeine
Hi, I'm using WSL2 as my programming environment and was wondering if this would cause issues when trying to partition the disk to read and write to it. So I have a few questions in regard to partitioning the disk.

First, should I partition the disk on my windows machine, or on the virtual disk? When looking at the partition table, is it a separate one for WSL or is it the same one as windows? (I tried to find what partition WSL was on, but I must not understand how it works because I could not find one).

Mainly, what's the process to making sure my OS is able to find a partition on the hard disk if it is running from WSL?

I'm sorry if I did not phrase that very well.

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 12:20 pm
by Octocontrabass
Which disk are you trying to partition?

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 12:32 pm
by iansjack
STOP, STOP, STOP!!!

Don’t mess with partitions if you are not absolutely sure what you are doing. (And it is obvious that you aren’t.)

Don’t touch your physical hard disk; create a virtual hard disk and use that. In Linux you would access this via a loop back device - a little Googling will provide a wealth of detail about this.

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 12:50 pm
by Caffeine
iansjack wrote:STOP, STOP, STOP!!!

Don’t mess with partitions if you are not absolutely sure what you are doing. (And it is obvious that you aren’t.)

Don’t touch your physical hard disk; create a virtual hard disk and use that. In Linux you would access this via a loop back device - a little Googling will provide a wealth of detail about this.
Thanks, I'll do that instead.

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 5:53 pm
by Caffeine
I created a virtual hard disk using this command:

Code: Select all

qemu-img create -f raw disk.img 2000M
And am running the OS with this:

Code: Select all

qemu-system-i386 -hda disk.img -cdrom Binaries/caffieneOS.iso -m 16M -boot order=dc
But when writing to the disk using my ata driver (the code is below) and restarting the OS, the virtual hard disk does not save my changes. Is there a reason that this is happening?

The ataPioWrite() function to write to hard disk:

Code: Select all

void ataPioWrite(unsigned int LBA, unsigned short sectorcount, unsigned char *target) {
    outportb(ATA_PRIMARY_DRIVE_HEAD, 0x40); // Select master
    outportb(ATA_PRIMARY_SECCOUNT, (sectorcount >> 8) & 0xFF);  // Sector Count hi
    outportb(ATA_PRIMARY_LBA_LO, (LBA << 24) & 0xFF); // LBA4
    outportb(ATA_PRIMARY_LBA_MID, (LBA << 32) & 0xFF);  //LBA5
    outportb(ATA_PRIMARY_LBA_HI, (LBA << 40) & 0xFF);  // LBA6

    outportb(ATA_PRIMARY_SECCOUNT, sectorcount & 0xFF); // Sector Count low
    outportb(ATA_PRIMARY_LBA_LO, LBA & 0xFF); // LBA1
    outportb(ATA_PRIMARY_LBA_MID, (LBA << 8) & 0xFF);  //LBA2
    outportb(ATA_PRIMARY_LBA_HI, (LBA << 16) & 0xFF);  // LBA3
    outportb(ATA_PRIMARY_COMM_REGSTAT, 0x34);  // WRITE SECTORS EXT command

    for (unsigned char i = 0; i < sectorcount; i++) {
        pollATA();  // Wait for it to be able to transfer data

        // Transfer the data
        for (int j = 0; j < 256; j++) { outportl(ATA_PRIMARY_DATA, target[i]); }
        target += 256;
    }

    // Flush the cache.
    outportb(ATA_PRIMARY_COMM_REGSTAT, 0xE7);
    // Poll for BSY.
    while (inportb(ATA_PRIMARY_COMM_REGSTAT) & STAT_BSY) {}
}

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 6:12 pm
by Octocontrabass
Do you see any compiler warnings? If not, turn on compiler warnings, then fix the mistakes in your code that cause the warnings to appear.

Do you see any QEMU warnings?

Which direction does "<<" shift the bits?

What size is the data port? What size does outportl write? What size is target?

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 6:56 pm
by Caffeine
Octocontrabass wrote:Do you see any compiler warnings? If not, turn on compiler warnings, then fix the mistakes in your code that cause the warnings to appear.
I've fixed all of the compiler warnings, so no luck with that
Octocontrabass wrote:Do you see any QEMU warnings?
Yes, the warning is:

Code: Select all

WARNING: Image format was not specified for 'disk.img' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
Octocontrabass wrote:Which direction does "<<" shift the bits?
I was shifting them the wrong way, thanks for catching that.
Octocontrabass wrote:What size is the data port? What size does outportl write? What size is target?

And here is the code for outportl:

Code: Select all

void outportl(unsigned int port, unsigned int value) {
	   __asm__ __volatile__("outl %%eax,%%dx"::"d" (port), "a" (value));
};

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 7:01 pm
by Octocontrabass
Caffeine wrote:And here is the code for outportl:
So outportl writes 32 bits. What size is the data port? What size is target?

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 7:23 pm
by Caffeine
Octocontrabass wrote:
Caffeine wrote:And here is the code for outportl:
So outportl writes 32 bits. What size is the data port? What size is target?


ATA_PRIMARY_DATA is defined as 0x1F0, and target (in this case) is 0x1.

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 7:43 pm
by Octocontrabass
Caffeine wrote:ATA_PRIMARY_DATA is defined as 0x1F0,
Yes, but how big is that port? What size of reads and writes does it accept?
Caffeine wrote:and target (in this case) is 0x1.

Yes, but how big is it? What would sizeof(target) be?

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 8:10 pm
by Caffeine
Octocontrabass wrote:
Caffeine wrote:ATA_PRIMARY_DATA is defined as 0x1F0,
Yes, but how big is that port? What size of reads and writes does it accept?
Caffeine wrote:and target (in this case) is 0x1.

Yes, but how big is it? What would sizeof(target) be?


target would be uint8_t, and the port accepts sizes of uint16_t.

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 10:35 pm
by Octocontrabass
So outportl writes a 32-bit value, but the data port only accepts 16-bit writes.

Re: How do I find a partition while in WSL2?

Posted: Wed Apr 06, 2022 10:47 pm
by Caffeine
Octocontrabass wrote:So outportl writes a 32-bit value, but the data port only accepts 16-bit writes.
I fixed, this, but the problem still isn't solved. The disk still does not get saved when I restart the OS. But thanks for catching the error!

Re: How do I find a partition while in WSL2?

Posted: Thu Apr 07, 2022 10:36 am
by Octocontrabass
How are you checking whether the disk gets saved? Which LBA are you writing to?

Why aren't you waiting until the drive is ready before sending the next command?

Re: How do I find a partition while in WSL2?

Posted: Thu Apr 07, 2022 11:14 am
by Caffeine
Octocontrabass wrote:How are you checking whether the disk gets saved? Which LBA are you writing to?
I'm writing to LBA 0x0 and I'm checking that the disk gets saved by restarting the OS and reading LBA 0x0 again. Is there a better way to check if the disk saved or not?
Octocontrabass wrote:Why aren't you waiting until the drive is ready before sending the next command?
I added this, (I assume you mean to poll again) and it's still not saving when I restart the disk.