How do I find a partition while in WSL2?

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.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

How do I find a partition while in WSL2?

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

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

Post by Octocontrabass »

Which disk are you trying to partition?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

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

Post 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.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

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

Post 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) {}
}
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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?
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

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

Post 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));
};
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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?
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

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

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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?
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

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

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

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

Post by Octocontrabass »

So outportl writes a 32-bit value, but the data port only accepts 16-bit writes.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

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

Post 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!
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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?
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

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

Post 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.
Post Reply