import struct

CHUNK    = 128*512
LV_START = 5120000*512
BSIZE    = 4096
IPG      = 8192
INODE_SZ = 256

def read_inode(inode_num):
    group    = (inode_num-1)//IPG
    idx      = (inode_num-1)%IPG
    it_block = 1070 + group*512
    it_virt  = it_block*BSIZE + idx*INODE_SZ

    grp      = it_virt//(5*CHUNK)
    in_group = it_virt%(5*CHUNK)
    chunk_idx= in_group//CHUNK
    intra    = in_group%CHUNK

    if chunk_idx == 4:
        return None, 'METADATA CHUNK'

    phys = LV_START + grp*4*CHUNK + chunk_idx*CHUNK + intra
    with open('/dev/sda','rb') as f:
        f.seek(phys)
        return f.read(INODE_SZ), 'OK'

def parse_inode(data):
    mode  = struct.unpack_from('<H', data,  0)[0]
    size  = struct.unpack_from('<I', data,  4)[0]
    mtime = struct.unpack_from('<I', data, 16)[0]
    links = struct.unpack_from('<H', data, 26)[0]
    flags = struct.unpack_from('<I', data, 32)[0]
    ext_magic = struct.unpack_from('<H', data, 40)[0]
    return mode, size, mtime, links, flags, ext_magic

# Check root inode and nearby system inodes
for ino in [2, 3, 4, 5, 6, 7, 8, 11, 12]:
    data, status = read_inode(ino)
    if data is None:
        print(f'Inode {ino:4d}: {status}')
        continue
    mode, size, mtime, links, flags, ext_magic = parse_inode(data)
    ftype = {0x4000:'dir', 0x8000:'file', 0xa000:'link'}.get(mode&0xf000,'?')
    print(f'Inode {ino:4d}: type={ftype} mode=0x{mode:04x} '
          f'size={size} links={links} '
          f'extent_magic=0x{ext_magic:04x} '
          f'mtime={mtime} [{status}]')
