/* warning : only works on data forks */ static int hfs_get_extent_containing (HfsPrivateFile* file, unsigned int block, HfsExtDataRec cache, uint16_t* ptr_start_cache) { uint8_t record[sizeof (HfsExtentKey) + sizeof (HfsExtDataRec)]; HfsExtentKey search; HfsExtentKey* ret_key = (HfsExtentKey*) record; HfsExtDescriptor* ret_cache = (HfsExtDescriptor*) (record + sizeof (HfsExtentKey)); HfsPrivateFSData* priv_data = (HfsPrivateFSData*) file->fs->type_specific; search.key_length = sizeof (HfsExtentKey) - 1; search.type = HFS_DATA_FORK; search.file_ID = file->CNID; search.start = PED_CPU_TO_BE16 (block); if (!hfs_btree_search (priv_data->extent_file, (HfsPrivateGenericKey*) &search, record, sizeof (record), NULL)) return 0; if (ret_key->file_ID != search.file_ID || ret_key->type != search.type) return 0; memcpy (cache, ret_cache, sizeof(HfsExtDataRec)); *ptr_start_cache = PED_BE16_TO_CPU (ret_key->start); return 1; }
/* Used by hfsplus_probe and hfs_probe */ PedGeometry* hfs_and_wrapper_probe (PedGeometry* geom) { uint8_t buf[PED_SECTOR_SIZE_DEFAULT]; HfsMasterDirectoryBlock *mdb; PedGeometry* geom_ret; PedSector search, max; PED_ASSERT (geom != NULL); PED_ASSERT (hfsc_can_use_geom (geom)); mdb = (HfsMasterDirectoryBlock *) buf; /* is 5 an intelligent value ? */ if ((geom->length < 5) || (!ped_geometry_read (geom, buf, 2, 1)) || (mdb->signature != PED_CPU_TO_BE16 (HFS_SIGNATURE)) ) return NULL; search = ((PedSector) PED_BE16_TO_CPU (mdb->start_block) + ((PedSector) PED_BE16_TO_CPU (mdb->total_blocks) * (PED_BE32_TO_CPU (mdb->block_size) / PED_SECTOR_SIZE_DEFAULT ))); max = search + (PED_BE32_TO_CPU (mdb->block_size) / PED_SECTOR_SIZE_DEFAULT); if (!(geom_ret = ped_geometry_new (geom->dev, geom->start, search + 2))) return NULL; for (; search < max; search++) { if (!ped_geometry_set (geom_ret, geom_ret->start, search + 2) || !ped_geometry_read (geom_ret, buf, search, 1)) break; if (mdb->signature == PED_CPU_TO_BE16 (HFS_SIGNATURE)) return geom_ret; } ped_geometry_destroy (geom_ret); return NULL; }
/* This function reads bad blocks extents in the extents file and store it in f.s. specific data of fs */ int hfs_read_bad_blocks (const PedFileSystem *fs) { HfsPrivateFSData* priv_data = (HfsPrivateFSData*) fs->type_specific; if (priv_data->bad_blocks_loaded) return 1; { uint8_t record[sizeof (HfsExtentKey) + sizeof (HfsExtDataRec)]; HfsExtentKey search; HfsExtentKey* ret_key = (HfsExtentKey*) record; HfsExtDescriptor* ret_cache = (HfsExtDescriptor*) (record + sizeof (HfsExtentKey)); unsigned int block, last_start, first_pass = 1; search.key_length = sizeof (HfsExtentKey) - 1; search.type = HFS_DATA_FORK; search.file_ID = PED_CPU_TO_BE32 (HFS_BAD_BLOCK_ID); last_start = -1; block = 0; while (1) { int i; search.start = PED_CPU_TO_BE16 (block); if (!hfs_btree_search (priv_data->extent_file, (HfsPrivateGenericKey*) &search, record, sizeof (record), NULL) || ret_key->file_ID != search.file_ID || ret_key->type != search.type) { if (first_pass) break; else goto errbb; } if (PED_BE16_TO_CPU (ret_key->start) == last_start) break; last_start = PED_BE16_TO_CPU (ret_key->start); for (i = 0; i < HFS_EXT_NB; i++) { if (ret_cache[i].block_count) { HfsPrivateLinkExtent* new_xt = (HfsPrivateLinkExtent*) ped_malloc ( sizeof (HfsPrivateLinkExtent)); if (!new_xt) goto errbb; new_xt->next = priv_data->bad_blocks_xtent_list; memcpy(&(new_xt->extent), ret_cache+i, sizeof (HfsExtDescriptor)); priv_data->bad_blocks_xtent_list = new_xt; priv_data->bad_blocks_xtent_nb++; block += PED_BE16_TO_CPU ( ret_cache[i].block_count); } } first_pass = 0; } priv_data->bad_blocks_loaded = 1; return 1;} errbb: hfs_free_bad_blocks_list(priv_data->bad_blocks_xtent_list); priv_data->bad_blocks_xtent_list=NULL; priv_data->bad_blocks_xtent_nb=0; return 0; }
static int dvh_write (const PedDisk* disk) { DVHDiskData* dvh_disk_data = disk->disk_specific; struct volume_header vh; int i; PED_ASSERT (dvh_disk_data != NULL); _flush_stale_flags (disk); memset (&vh, 0, sizeof (struct volume_header)); vh.vh_magic = PED_CPU_TO_BE32 (VHMAGIC); vh.vh_rootpt = PED_CPU_TO_BE16 (dvh_disk_data->root - 1); vh.vh_swappt = PED_CPU_TO_BE16 (dvh_disk_data->swap - 1); if (dvh_disk_data->boot) { PedPartition* boot_part; boot_part = ped_disk_get_partition (disk, dvh_disk_data->boot); strcpy (vh.vh_bootfile, ped_partition_get_name (boot_part)); } vh.vh_dp = dvh_disk_data->dev_params; /* Set up rudimentary device geometry */ vh.vh_dp.dp_cyls = PED_CPU_TO_BE16 ((short)disk->dev->bios_geom.cylinders); vh.vh_dp.dp_trks0 = PED_CPU_TO_BE16 ((short)disk->dev->bios_geom.heads); vh.vh_dp.dp_secs = PED_CPU_TO_BE16 ((short)disk->dev->bios_geom.sectors); vh.vh_dp.dp_secbytes = PED_CPU_TO_BE16 ((short)disk->dev->sector_size); for (i = 0; i < NPARTAB; i++) { PedPartition* part = ped_disk_get_partition (disk, i + 1); if (part) _generate_partition (part, &vh.vh_pt[i]); } /* whole disk partition * This is only ever written here, and never modified * (or even shown) as it must contain the entire disk, * and parted does not like overlapping partitions */ vh.vh_pt[PNUM_VOLUME].pt_nblks = PED_CPU_TO_BE32 (disk->dev->length); vh.vh_pt[PNUM_VOLUME].pt_firstlbn = PED_CPU_TO_BE32 (0); vh.vh_pt[PNUM_VOLUME].pt_type = PED_CPU_TO_BE32 (PTYPE_VOLUME); for (i = 0; i < NVDIR; i++) { PedPartition* part = ped_disk_get_partition (disk, i + 1 + NPARTAB); if (part) _generate_boot_file (part, &vh.vh_vd[i]); } vh.vh_csum = 0; vh.vh_csum = PED_CPU_TO_BE32 (_checksum ((uint32_t*) &vh, sizeof (struct volume_header))); return (ptt_write_sector (disk, &vh, sizeof vh) && ped_device_sync (disk->dev)); }
/* Update the HFS wrapper mdb and bad blocks file to reflect the new geometry of the embedded HFS+ volume */ static int hfsplus_wrapper_update (PedFileSystem* fs) { uint8_t node[PED_SECTOR_SIZE_DEFAULT]; HfsCPrivateLeafRec ref; HfsExtentKey key; HfsNodeDescriptor* node_desc = (HfsNodeDescriptor*) node; HfsExtentKey* ret_key; HfsExtDescriptor* ret_data; unsigned int i; HfsPPrivateFSData* priv_data = (HfsPPrivateFSData*) fs->type_specific; HfsPrivateFSData* hfs_priv_data = (HfsPrivateFSData*) priv_data->wrapper->type_specific; unsigned int hfs_sect_block = PED_BE32_TO_CPU (hfs_priv_data->mdb->block_size) / PED_SECTOR_SIZE_DEFAULT ; PedSector hfsplus_sect = (PedSector) PED_BE32_TO_CPU (priv_data->vh->total_blocks) * ( PED_BE32_TO_CPU (priv_data->vh->block_size) / PED_SECTOR_SIZE_DEFAULT ); unsigned int hfs_blocks_embedded = (hfsplus_sect + hfs_sect_block - 1) / hfs_sect_block; unsigned int hfs_blocks_embedded_old; /* update HFS wrapper MDB */ hfs_blocks_embedded_old = PED_BE16_TO_CPU ( hfs_priv_data->mdb->old_new .embedded.location.block_count ); hfs_priv_data->mdb->old_new.embedded.location.block_count = PED_CPU_TO_BE16 (hfs_blocks_embedded); /* maybe macOS will boot with this */ /* update : yes it does \o/ :) */ hfs_priv_data->mdb->free_blocks = PED_CPU_TO_BE16 ( PED_BE16_TO_CPU (hfs_priv_data->mdb->free_blocks) + hfs_blocks_embedded_old - hfs_blocks_embedded ); if (!hfs_update_mdb(priv_data->wrapper)) return 0; /* force reload bad block list */ if (hfs_priv_data->bad_blocks_loaded) { hfs_free_bad_blocks_list (hfs_priv_data->bad_blocks_xtent_list); hfs_priv_data->bad_blocks_xtent_list = NULL; hfs_priv_data->bad_blocks_xtent_nb = 0; hfs_priv_data->bad_blocks_loaded = 0; } /* clean HFS wrapper allocation map */ for (i = PED_BE16_TO_CPU ( hfs_priv_data->mdb->old_new.embedded .location.start_block ) + hfs_blocks_embedded; i < PED_BE16_TO_CPU ( hfs_priv_data->mdb->old_new.embedded .location.start_block ) + hfs_blocks_embedded_old; i++ ) { CLR_BLOC_OCCUPATION(hfs_priv_data->alloc_map, i); } /* and save it */ if (!ped_geometry_write (fs->geom, hfs_priv_data->alloc_map, PED_BE16_TO_CPU ( hfs_priv_data->mdb->volume_bitmap_block ), ( PED_BE16_TO_CPU ( hfs_priv_data->mdb->total_blocks ) + PED_SECTOR_SIZE_DEFAULT * 8 - 1 ) / (PED_SECTOR_SIZE_DEFAULT * 8))) return 0; if (!ped_geometry_sync (fs->geom)) return 0; /* search and update the bad blocks file */ key.key_length = sizeof(key) - 1; key.type = HFS_DATA_FORK; key.file_ID = PED_CPU_TO_BE32 (HFS_BAD_BLOCK_ID); key.start = 0; if (!hfs_btree_search (hfs_priv_data->extent_file, (HfsPrivateGenericKey*) &key, NULL, 0, &ref)) { ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("An error occurred while looking for the mandatory " "bad blocks file.")); return 0; } if (!hfs_file_read_sector (hfs_priv_data->extent_file, node, ref.node_number)) return 0; ret_key = (HfsExtentKey*) (node + ref.record_pos); ret_data = (HfsExtDescriptor*) ( node + ref.record_pos + sizeof (HfsExtentKey) ); while (ret_key->type == key.type && ret_key->file_ID == key.file_ID) { for (i = 0; i < HFS_EXT_NB; i++) { if ( ret_data[i].start_block == hfs_priv_data->mdb->old_new .embedded.location.start_block) { ret_data[i].block_count = hfs_priv_data->mdb->old_new .embedded.location.block_count; /* found ! : update */ if (!hfs_file_write_sector ( hfs_priv_data->extent_file, node, ref.node_number) || !ped_geometry_sync(fs->geom)) return 0; return 1; } } if (ref.record_number < PED_BE16_TO_CPU (node_desc->rec_nb)) { ref.record_number++; } else { ref.node_number = PED_BE32_TO_CPU (node_desc->next); if (!ref.node_number || !hfs_file_read_sector(hfs_priv_data->extent_file, node, ref.node_number)) goto bb_not_found; ref.record_number = 1; } ref.record_pos = PED_BE16_TO_CPU (*((uint16_t *) (node + (PED_SECTOR_SIZE_DEFAULT - 2*ref.record_number)))); ret_key = (HfsExtentKey*) (node + ref.record_pos); ret_data = (HfsExtDescriptor*) ( node + ref.record_pos + sizeof (HfsExtentKey) ); } bb_not_found: /* not found : not a valid hfs+ wrapper : failure */ ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("It seems there is an error in the HFS wrapper: the bad " "blocks file doesn't contain the embedded HFS+ volume.")); return 0; }
PedFileSystem* hfsplus_open (PedGeometry* geom) { uint8_t buf[PED_SECTOR_SIZE_DEFAULT]; PedFileSystem* fs; HfsPVolumeHeader* vh; HfsPPrivateFSData* priv_data; PedGeometry* wrapper_geom; unsigned int map_sectors; if (!hfsc_can_use_geom (geom)) return NULL; fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem)); if (!fs) goto hpo; vh = (HfsPVolumeHeader*) ped_malloc (sizeof (HfsPVolumeHeader)); if (!vh) goto hpo_fs; priv_data = (HfsPPrivateFSData*)ped_malloc (sizeof (HfsPPrivateFSData)); if (!priv_data) goto hpo_vh; fs->geom = ped_geometry_duplicate (geom); if (!fs->geom) goto hpo_pd; fs->type_specific = (void*) priv_data; if ((wrapper_geom = hfs_and_wrapper_probe (geom))) { HfsPrivateFSData* hfs_priv_data; PedSector abs_sect, length; unsigned int bs; ped_geometry_destroy (wrapper_geom); priv_data->wrapper = hfs_open(geom); if (!priv_data->wrapper) goto hpo_gm; hfs_priv_data = (HfsPrivateFSData*) priv_data->wrapper->type_specific; bs = PED_BE32_TO_CPU (hfs_priv_data->mdb->block_size) / PED_SECTOR_SIZE_DEFAULT; abs_sect = (PedSector) geom->start + (PedSector) PED_BE16_TO_CPU ( hfs_priv_data->mdb->start_block) + (PedSector) PED_BE16_TO_CPU ( hfs_priv_data->mdb->old_new .embedded.location.start_block ) * bs; length = (PedSector) PED_BE16_TO_CPU ( hfs_priv_data->mdb->old_new .embedded.location.block_count) * bs; priv_data->plus_geom = ped_geometry_new (geom->dev, abs_sect, length); if (!priv_data->plus_geom) goto hpo_wr; priv_data->free_geom = 1; } else { priv_data->wrapper = NULL; priv_data->plus_geom = fs->geom; priv_data->free_geom = 0; } if (!ped_geometry_read (priv_data->plus_geom, buf, 2, 1)) goto hpo_pg; memcpy (vh, buf, sizeof (HfsPVolumeHeader)); priv_data->vh = vh; if (vh->signature != PED_CPU_TO_BE16(HFSP_SIGNATURE) && vh->signature != PED_CPU_TO_BE16(HFSX_SIGNATURE)) { ped_exception_throw ( PED_EXCEPTION_BUG, PED_EXCEPTION_CANCEL, _("No valid HFS[+X] signature has been found while " "opening.")); goto hpo_pg; } if (vh->signature == PED_CPU_TO_BE16(HFSP_SIGNATURE) && vh->version != PED_CPU_TO_BE16(HFSP_VERSION)) { if (ped_exception_throw ( PED_EXCEPTION_NO_FEATURE, PED_EXCEPTION_IGNORE_CANCEL, _("Version %d of HFS+ isn't supported."), PED_BE16_TO_CPU(vh->version)) != PED_EXCEPTION_IGNORE) goto hpo_pg; } if (vh->signature == PED_CPU_TO_BE16(HFSX_SIGNATURE) && vh->version != PED_CPU_TO_BE16(HFSX_VERSION)) { if (ped_exception_throw ( PED_EXCEPTION_NO_FEATURE, PED_EXCEPTION_IGNORE_CANCEL, _("Version %d of HFSX isn't supported."), PED_BE16_TO_CPU(vh->version)) != PED_EXCEPTION_IGNORE) goto hpo_pg; } priv_data->jib_start_block = 0; priv_data->jl_start_block = 0; if (vh->attributes & PED_CPU_TO_BE32(1<<HFSP_JOURNALED)) { if (!hfsj_replay_journal(fs)) goto hpo_pg; } priv_data->bad_blocks_loaded = 0; priv_data->bad_blocks_xtent_nb = 0; priv_data->bad_blocks_xtent_list = NULL; priv_data->extents_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFS_XTENT_ID), vh->extents_file.extents, PED_BE64_TO_CPU ( vh->extents_file.logical_size ) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->extents_file) goto hpo_pg; priv_data->catalog_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFS_CATALOG_ID), vh->catalog_file.extents, PED_BE64_TO_CPU ( vh->catalog_file.logical_size ) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->catalog_file) goto hpo_ce; priv_data->attributes_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFSP_ATTRIB_ID), vh->attributes_file.extents, PED_BE64_TO_CPU ( vh->attributes_file.logical_size) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->attributes_file) goto hpo_cc; map_sectors = ( PED_BE32_TO_CPU (vh->total_blocks) + PED_SECTOR_SIZE_DEFAULT * 8 - 1 ) / (PED_SECTOR_SIZE_DEFAULT * 8); priv_data->dirty_alloc_map = (uint8_t*) ped_malloc ((map_sectors + 7) / 8); if (!priv_data->dirty_alloc_map) goto hpo_cl; memset(priv_data->dirty_alloc_map, 0, (map_sectors + 7) / 8); priv_data->alloc_map = (uint8_t*) ped_malloc (map_sectors * PED_SECTOR_SIZE_DEFAULT); if (!priv_data->alloc_map) goto hpo_dm; priv_data->allocation_file = hfsplus_file_open (fs, PED_CPU_TO_BE32 (HFSP_ALLOC_ID), vh->allocation_file.extents, PED_BE64_TO_CPU ( vh->allocation_file.logical_size) / PED_SECTOR_SIZE_DEFAULT); if (!priv_data->allocation_file) goto hpo_am; if (!hfsplus_file_read (priv_data->allocation_file, priv_data->alloc_map, 0, map_sectors)) { hfsplus_close(fs); return NULL; } fs->type = &hfsplus_type; fs->checked = ((PED_BE32_TO_CPU (vh->attributes) >> HFS_UNMOUNTED) & 1) && !((PED_BE32_TO_CPU (vh->attributes) >> HFSP_INCONSISTENT) & 1); return fs; /*--- clean error handling ---*/ hpo_am: free(priv_data->alloc_map); hpo_dm: free(priv_data->dirty_alloc_map); hpo_cl: hfsplus_file_close (priv_data->attributes_file); hpo_cc: hfsplus_file_close (priv_data->catalog_file); hpo_ce: hfsplus_file_close (priv_data->extents_file); hpo_pg: if (priv_data->free_geom) ped_geometry_destroy (priv_data->plus_geom); hpo_wr: if (priv_data->wrapper) hfs_close(priv_data->wrapper); hpo_gm: ped_geometry_destroy (fs->geom); hpo_pd: free(priv_data); hpo_vh: free(vh); hpo_fs: free(fs); hpo: return NULL; }
int hfs_resize (PedFileSystem* fs, PedGeometry* geom, PedTimer* timer) { uint8_t buf[PED_SECTOR_SIZE_DEFAULT]; unsigned int nblock, nfree; unsigned int block, to_free; HfsPrivateFSData* priv_data; HfsMasterDirectoryBlock* mdb; int resize = 1; unsigned int hfs_sect_block; PedSector hgee; /* check preconditions */ PED_ASSERT (fs != NULL); PED_ASSERT (fs->geom != NULL); PED_ASSERT (geom != NULL); #ifdef DEBUG PED_ASSERT ((hgee = hfs_get_empty_end(fs)) != 0); #else if ((hgee = hfs_get_empty_end(fs)) == 0) return 0; #endif PED_ASSERT ((hgee = hfs_get_empty_end(fs)) != 0); if (ped_geometry_test_equal(fs->geom, geom)) return 1; priv_data = (HfsPrivateFSData*) fs->type_specific; mdb = priv_data->mdb; hfs_sect_block = PED_BE32_TO_CPU (mdb->block_size) / PED_SECTOR_SIZE_DEFAULT; if (fs->geom->start != geom->start || geom->length > fs->geom->length || geom->length < hgee + 2) { ped_exception_throw ( PED_EXCEPTION_NO_FEATURE, PED_EXCEPTION_CANCEL, _("Sorry, HFS cannot be resized that way yet.")); return 0; } /* Flush caches */ if (!ped_geometry_sync(fs->geom)) return 0; /* Clear the unmounted bit */ mdb->volume_attributes &= PED_CPU_TO_BE16 (~( 1 << HFS_UNMOUNTED )); if (!ped_geometry_read (fs->geom, buf, 2, 1)) return 0; memcpy (buf, mdb, sizeof (HfsMasterDirectoryBlock)); if ( !ped_geometry_write (fs->geom, buf, 2, 1) || !ped_geometry_sync (fs->geom)) return 0; ped_timer_reset (timer); ped_timer_set_state_name(timer, _("shrinking")); ped_timer_update(timer, 0.0); /* relocate data */ to_free = ( fs->geom->length - geom->length + hfs_sect_block - 1 ) / hfs_sect_block ; block = hfs_find_start_pack (fs, to_free); if (!hfs_pack_free_space_from_block (fs, block, timer, to_free)) { resize = 0; ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Data relocation has failed.")); goto write_MDB; } /* Calculate new block number and other MDB field */ nblock = ( geom->length - (PED_BE16_TO_CPU (mdb->start_block) + 2) ) / hfs_sect_block; nfree = PED_BE16_TO_CPU (mdb->free_blocks) - ( PED_BE16_TO_CPU (mdb->total_blocks) - nblock ); /* Check that all block after future end are really free */ for (block = nblock; block < PED_BE16_TO_CPU (mdb->total_blocks); block++) { if (TST_BLOC_OCCUPATION(priv_data->alloc_map,block)) { resize = 0; ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Data relocation left some data in the end " "of the volume.")); goto write_MDB; } } /* Mark out of volume blocks as used (broken implementations compatibility) */ for ( block = nblock; block < (1 << 16); ++block) SET_BLOC_OCCUPATION(priv_data->alloc_map,block); /* save the allocation map I do not write until start of allocation blocks but only until pre-resize end of bitmap blocks because the specifications do _not_ assert that everything until allocation blocks is boot, mdb and alloc */ ped_geometry_write(fs->geom, priv_data->alloc_map, PED_BE16_TO_CPU (priv_data->mdb->volume_bitmap_block), ( PED_BE16_TO_CPU (priv_data->mdb->total_blocks) + PED_SECTOR_SIZE_DEFAULT * 8 - 1) / (PED_SECTOR_SIZE_DEFAULT * 8)); /* Update geometry */ if (resize) { /* update in fs structure */ if (PED_BE16_TO_CPU (mdb->next_allocation) >= nblock) mdb->next_allocation = PED_CPU_TO_BE16 (0); mdb->total_blocks = PED_CPU_TO_BE16 (nblock); mdb->free_blocks = PED_CPU_TO_BE16 (nfree); /* update parted structure */ fs->geom->length = geom->length; fs->geom->end = fs->geom->start + geom->length - 1; } /* Set the unmounted bit */ mdb->volume_attributes |= PED_CPU_TO_BE16 ( 1 << HFS_UNMOUNTED ); /* Effective write */ write_MDB: ped_timer_set_state_name(timer,_("writing HFS Master Directory Block")); if (!hfs_update_mdb(fs)) { ped_geometry_sync(geom); return 0; } if (!ped_geometry_sync(geom)) return 0; ped_timer_update(timer, 1.0); return (resize); }