Page 1 of 1

File system image mkfs.c XV6

Posted: Tue Jun 20, 2023 11:46 am
by Crixus
Hello,
There is something that i cant understand.

Why all numerical data in mkfs.c XV6 file system image creator, are converted from big<-->little endian and vice versa.

example :
in the superblock :

Code: Select all

    sb.size = xint(FSSIZE);
    sb.nblocks = xint(nblocks);
    sb.ninodes = xint(NINODES);
    sb.nlog = xint(nlog);
    sb.logstart = xint(2);
    sb.inodestart = xint(2 + nlog);
    sb.bmapstart = xint(2 + nlog + ninodeblocks);
then :

Code: Select all

uint xint(uint x) {
    uint y;
    uchar* a = (uchar*)&y;
    a[0] = x;
    a[1] = x >> 8;
    a[2] = x >> 16;
    a[3] = x >> 24;
    return y;
}
I have try to figure out why but it is pretty closed subject.

I hope this question relevant.

Re: File system image mkfs.c XV6

Posted: Tue Jun 20, 2023 6:03 pm
by Octocontrabass
Crixus wrote:Why all numerical data in mkfs.c XV6 file system image creator, are converted from big<-->little endian and vice versa.
It converts between host-endian and little-endian. On a big-endian host, it converts between big-endian and little-endian. On a little-endian host, the conversion doesn't do anything.

It does that because mkfs needs to be able to run on both big-endian and little-endian CPUs, but the filesystem is always little-endian.

Re: File system image mkfs.c XV6

Posted: Tue Jun 20, 2023 7:26 pm
by Crixus
like that ?

Assuming 0x12345678:

On a Big Endian system:
Original x in memory: [12][34][56][78]
After xint(), y in memory: [78][56][34][12] (bytes are reversed)
This will appear as a change when viewing memory.

On a Little Endian system:
Original x in memory: [78][56][34][12]
After xint(), y in memory: [78][56][34][12] (bytes stay in same order)
This will appear as no change when viewing memory.


And so the disk image is in little endian... when a big endian proc have to use it, it should reverse all numerical data every times ?

Re: File system image mkfs.c XV6

Posted: Tue Jun 20, 2023 10:20 pm
by Octocontrabass
Crixus wrote:like that ?
Yep, you've got it.
Crixus wrote:And so the disk image is in little endian... when a big endian proc have to use it, it should reverse all numerical data every times ?
Yes, but xv6 only runs on little-endian CPUs.