C - fseek ohne fseek - OS dev. Strktur auslesen ab Sektor 23 Position 108

jkallup

Erfahrenes Mitglied
Hallo,

habe vor, mein Eignes OS zu programmieren.
Jetzt bin ich soweit, um ein Verzeichnis von einen CD Rom ISO Image auszulesen.
Nagut, fast :)

wie man hier sehen kann, lese ich eine Struktur ab Sektor 23 einer CD aus:
Code:
struct directory_record
{
  char res1[108]; //res1[108+46]; +46 = nächster Eintrag

  WORD version;
  WORD sector;
  WORD sector_opt1;
  WORD sector_opt2;
  WORD sector_opt3;

  WORD length;
  WORD length_opt1;
  WORD length_opt2;
  WORD length_opt3;

  BYTE year;
  BYTE month;
  BYTE day;

  BYTE hour;
  BYTE minute;
  BYTE second;

  BYTE res2;
  BYTE flag;

  char res3[6];

  uint8_t ident_length;
  char ident[12];
} DIR_RECORD;


struct disk_packet
{
    byte size_pack;
    byte reserved1;
    byte no_of_blocks;
    byte reserved2;
    word offset;
    word segment;
    dword lba1;
    dword lba2;
} __attribute__((packed)) disk_pack;


void LBASectorRead(void *buffer, unsigned long lba)
{
    unsigned char bError = 0;
    REGS regs;

    disk_pack.size_pack = 16;
    disk_pack.no_of_blocks = 1;
    disk_pack.reserved1 = 0;
    disk_pack.reserved2 = 0;
    disk_pack.segment = (((unsigned int)buffer >> 16) << 12);
    disk_pack.offset = ((unsigned int)buffer & 0xffff);
    disk_pack.lba2 = lba >> 32;
    disk_pack.lba1 = lba & 0xffffffff;

regs.b.ds = (((unsigned int)&disk_pack >> 16) << 12);
regs.b.si = ((unsigned int)&disk_pack &0xffff);
regs.b.dl = 0x9f;
regs.b.ah = 0x42;
int386(0x13,&regs,&regs);
}


struct directory_record dr[2];
LBASectorRead( &dr,23);

printf("Verzeichnisstruktur .root:");
nl();

int count;
for (count = 0; count < 3; count++)
{
//if (dr[count].version == 0) break;

if (dr[count].flag == 0)
{ dr[count].ident[dr[count].ident_length-2] = '\0'; } else
{ dr[count].ident[dr[count].ident_length-0] = '\0'; }

printf("%02d.%02d.%02d %02d:%02d:%02d %8d %s",
dr[count].day,
dr[count].month,
dr[count].year+1900,
dr[count].hour,
dr[count].minute,
dr[count].second,
dr[count].length,
dr[count].ident);

nl();
}

Leider ist aber ein Verzeichnis Eintrag 46 Byte groß.
Also müsste ich beim nächsten einlesen 108 + 46 Byte vorwärts gehen.
Wenn ich das wie in den Code mache, dan reserviere ich ja Speicher (2 * 108) ****?
anstatt 2 * 46.

Wie ist es nun möglich, die Struktur Member res1[108] aus der Struktur zu entfernen,
so dass die dann wirklich nur 46 Bytes groß ist.
Es müsste also eiine Impplementation eines "einfachen" fseek, die dann auf Sektor 23
zeigt, 108 Bytes einliesst, geben ....

Hat da einer von Euch eine Idee?
Danke schonmal
 

Anhänge

  • dir.JPG
    dir.JPG
    52,1 KB · Aufrufe: 9
So,

nach langen hin und her, habe ich es hinbekommen, ein einfaches seek zu programmieren.
hier das Ergebins:

Code:
struct disk_packet
{
    byte size_pack;
    byte reserved1;
    byte no_of_blocks;
    byte reserved2;
    word offset;
    word segment;
    dword lba1;
    dword lba2;
} __attribute__((packed)) disk_pack;

struct directory_record
{
  WORD		version;
  WORD		sector;
  WORD		sector_opt1;
  WORD		sector_opt2;
  WORD		sector_opt3;

  WORD	 	length;
  WORD		length_opt1;
  WORD		length_opt2;
  WORD		length_opt3;

  BYTE		year;
  BYTE		month;
  BYTE		day;

  BYTE		hour;
  BYTE		minute;
  BYTE		second;

  BYTE		res2;
  BYTE		flag;

  char		res3[6];

  uint8_t	ident_length;
  char		ident[12];

  BYTE		padding;
} __attribute__((packed)) DIR_RECORD;



void LBASectorRead(void *buffer, unsigned long lba)
{
    unsigned char bError = 0;
    REGS regs;

    disk_pack.size_pack = 16;
    disk_pack.no_of_blocks = 1;
    disk_pack.reserved1 = 0;
    disk_pack.reserved2 = 0;
    disk_pack.segment = (((unsigned int)buffer >> 16) << 12);
    disk_pack.offset  =  ((unsigned int)buffer & 0xffff);
    disk_pack.lba2 = lba >> 32;
    disk_pack.lba1 = lba & 0xffffffff;

	regs.b.ds = (((unsigned int)&disk_pack >> 16) << 12);
	regs.b.si =  ((unsigned int)&disk_pack &0xffff);
	regs.b.dl = 0x9f;
	regs.b.ah = 0x42;
	int386(0x13,&regs,&regs);
}

char *malloc_ptr;
void *malloc(int size)
{
  void *ret;
  ret = (void*)malloc_ptr;
  malloc_ptr += size;
  return ret;
}

struct StreamHandler
{
  int		cursorPosition;
  char* 	data;
  unsigned int  data_size;
};
typedef struct StreamHandler FILE;


void seek(FILE* handle, int bytes)
{
  handle->cursorPosition += bytes;
}

char getc(FILE* handle)
{
  int pos = handle->cursorPosition;
  handle->cursorPosition++;
  return handle->data[pos];
}

int read(FILE* handle, char* out, unsigned int bytes)
{
  memcpy(out, handle->data + handle->cursorPosition, bytes);
  return bytes;
}

void zeige_dir(void)
{
	struct directory_record dr[3];
	FILE *f;
	char buffer[2048] = { 0 };
	char nil[108];

	LBASectorRead(buffer,23);

	f->cursorPosition = 0;
	f->data = buffer;
	f->data_size = 2048;

	seek(f,108);
        read(f,(char*)&dr,sizeof(DIR_RECORD)*3);

	int count;
	for (count = 0; count < 3; count++)
	{
		if (dr[count].flag == 0)
		{   dr[count].ident[dr[count].ident_length-2] = '\0'; } else
		{   dr[count].ident[dr[count].ident_length-0] = '\0'; }

		printf("%02d.%02d.%02d  %02d:%02d:%02d  %8d %s\n",
		dr[count].day,
		dr[count].month,
		dr[count].year+1900,
		dr[count].hour,
		dr[count].minute,
		dr[count].second,
		dr[count].length,
		dr[count].ident);
	}
}
 

Anhänge

  • dir.JPG
    dir.JPG
    53,1 KB · Aufrufe: 9
Zurück