#include #include #include #include #include "mbr.h" #include "volumes.h" #include "disk.h" static struct mbr_desc_t loaded_mbr; int load_mbr(struct mbr_desc_t *mbr) { struct dskinfo_t info = dskinfo(); uintptr_t *sector; /* Le mbr n'a jamais été lu, le lire depuis le disque */ if ( loaded_mbr.magic_key != MBR_MAGIC ) { sector = malloc(info.taille_sec); read_sector(0, 0, sector); memcpy(mbr, sector, sizeof(struct mbr_desc_t)); free(sector); /* Si le MBR est incorrect, on en recrée un */ if ( mbr->magic_key != MBR_MAGIC ) { mbr->magic_key = MBR_MAGIC; mbr->vol_count = 0; /* Mettre en cache */ memcpy(&loaded_mbr, mbr, sizeof(struct mbr_desc_t)); return MBR_FAILURE; } /* Mettre en cache */ memcpy(&loaded_mbr, mbr, sizeof(struct mbr_desc_t)); } /* On avait déjà lu le MBR */ else memcpy(mbr, &loaded_mbr, sizeof(struct mbr_desc_t)); return MBR_SUCCESS; } int save_mbr(struct mbr_desc_t *mbr) { struct dskinfo_t info = dskinfo(); uintptr_t *sector; /* Ecrire sur le disque */ sector = malloc(info.taille_sec); memcpy(sector, mbr, sizeof(struct mbr_desc_t)); write_sector(0, 0, sector); /* Mettre à jour le cache */ memcpy(&loaded_mbr, mbr, sizeof(struct mbr_desc_t)); free(sector); return MBR_SUCCESS; } /*** DEBUG ***/ void dump_mbr(struct mbr_desc_t *mbr) { int i; printf("Magic key : %x\n", mbr->magic_key); printf("Name : %s\n", mbr->name); printf("Volume count : %u\n", mbr->vol_count); for(i=0;ivol_count;i++) dump_volume(&mbr->volume[i]); }