zircon/system/ulib/fs-management/fvm.cc
// Helper function to overwrite FVM given the slice_size
Helper function to overwrite FVM given the slice_size
Helper function to overwrite FVM given the slice_size
zx_status_t fvm_overwrite_impl(const fbl::unique_fd& fd, size_t slice_size) {
  fuchsia_hardware_block_BlockInfo block_info;
  fdio_cpp::UnownedFdioCaller disk_connection(fd.get());
  zx::unowned_channel channel(disk_connection.borrow_channel());
  zx_status_t status;
  zx_status_t io_status = fuchsia_hardware_block_BlockGetInfo(channel->get(), &status, &block_info);
  if (io_status != ZX_OK) {
    return io_status;
  } else if (status != ZX_OK) {
    return status;
  }
  size_t disk_size = block_info.block_count * block_info.block_size;
  size_t metadata_size = fvm::MetadataSize(disk_size, slice_size);
  std::unique_ptr<uint8_t[]> buf(new uint8_t[metadata_size]);
  memset(buf.get(), 0, metadata_size);
  if (lseek(fd.get(), 0, SEEK_SET) < 0) {
    return ZX_ERR_IO;
  }
  if (write(fd.get(), buf.get(), metadata_size) != static_cast<ssize_t>(metadata_size)) {
    fprintf(stderr, "fvm_overwrite_impl: Failed to write metadata\n");
    return ZX_ERR_IO;
  }
  if (write(fd.get(), buf.get(), metadata_size) != static_cast<ssize_t>(metadata_size)) {
    fprintf(stderr, "fvm_overwrite_impl: Failed to write metadata (secondary)\n");
    return ZX_ERR_IO;
  }
  io_status = fuchsia_hardware_block_BlockRebindDevice(channel->get(), &status);
  if (io_status != ZX_OK) {
    return io_status;
  }
  return status;
}
zx_status_t fvm_overwrite_impl ( const fbl :: unique_fd & fd , size_t slice_size ) { fuchsia_hardware_block_BlockInfo block_info ; fdio_cpp :: UnownedFdioCaller disk_connection ( fd . get ( ) ) ; zx :: unowned_channel channel ( disk_connection . borrow_channel ( ) ) ; zx_status_t status ; zx_status_t io_status = fuchsia_hardware_block_BlockGetInfo ( channel -> get ( ) , & status , & block_info ) ; if ( io_status != ZX_OK ) { return io_status ; } else if ( status != ZX_OK ) { return status ; } size_t disk_size = block_info . block_count * block_info . block_size ; size_t metadata_size = fvm :: MetadataSize ( disk_size , slice_size ) ; std :: unique_ptr < uint8_t [ ] > buf ( new uint8_t [ metadata_size ] ) ; memset ( buf . get ( ) , 0 , metadata_size ) ; if ( lseek ( fd . get ( ) , 0 , SEEK_SET ) < 0 ) { return ZX_ERR_IO ; } if ( write ( fd . get ( ) , buf . get ( ) , metadata_size ) != static_cast < ssize_t > ( metadata_size ) ) { fprintf ( stderr , " \n " ) ; return ZX_ERR_IO ; } if ( write ( fd . get ( ) , buf . get ( ) , metadata_size ) != static_cast < ssize_t > ( metadata_size ) ) { fprintf ( stderr , " \n " ) ; return ZX_ERR_IO ; } io_status = fuchsia_hardware_block_BlockRebindDevice ( channel -> get ( ) , & status ) ; if ( io_status != ZX_OK ) { return io_status ; } return status ; }
Helper function to overwrite FVM given the slice_size
Helper function to overwrite FVM given the slice_size
// Write to primary copy. // Write to backup copy