Ejemplo n.º 1
0
static int
xbsd_writelabel(struct partition *p)
{
    struct xbsd_disklabel *d = &xbsd_dlabel;
    unsigned int sector;

#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
    sector = get_start_sect(p) + BSD_LABELSECTOR;
#else
    (void)p; /* silence warning */
    sector = BSD_LABELSECTOR;
#endif

    d->d_checksum = 0;
    d->d_checksum = xbsd_dkcksum(d);

    /* This is necessary if we want to write the bootstrap later,
       otherwise we'd write the old disklabel with the bootstrap.
    */
    memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
            d, sizeof(struct xbsd_disklabel));

#if defined(__alpha__) && BSD_LABELSECTOR == 0
    alpha_bootblock_checksum(disklabelbuffer);
    seek_sector(0);
    xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
#else
    seek_sector(sector);
    lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
    xwrite(dev_fd, d, sizeof(*d));
#endif
    sync_disks();
    return 1;
}
Ejemplo n.º 2
0
void read_sector_n(const unsigned int cylinder, 
		   const unsigned int sector, 
		   unsigned char* buffer, 
		   const size_t n){

  int i;  


  if( !seek_sector(cylinder, sector) ){
    buffer = NULL;
    return;
  }


  /* Mettre dans MASTERBUFFER les donnees a cette emplacement */
  _out(HDA_DATAREGS, 1);
  _out(HDA_CMDREG, CMD_READ);
  _sleep(HDA_IRQ);


  /* exploiter les donnees de MASTERBUFFER */
  for(i=0; i<HDA_SECTORSIZE && i<n; i++){
    buffer[i] = (unsigned char)MASTERBUFFER[i];
  }
}
Ejemplo n.º 3
0
void read_sector(unsigned int cylinder, unsigned int sector, unsigned char* buffer){

  int secteur_size;
  int tmp, i;  


  if( !seek_sector(cylinder, sector) ){
    buffer = NULL;
    return;
  }


  /* Mettre dans MASTERBUFFER les donnees a cette emplacement */
  _out(HDA_DATAREGS, 1);
  _out(HDA_CMDREG, CMD_READ);
  _sleep(HDA_IRQ);


  /* exploiter les donnees de MASTERBUFFER */
  _out(HDA_CMDREG, CMD_DSKINFO);

  tmp = _in(HDA_DATAREGS+4);
  tmp = tmp<<8;
  secteur_size = tmp + _in(HDA_DATAREGS+5);

  for(i=0; i<secteur_size; i++){
    buffer[i] = (unsigned char)MASTERBUFFER[i];
  }

  buffer[++i] = EOF;
}
Ejemplo n.º 4
0
static DVDA_Packet_Reader*
open_packet_reader(DVDA_Sector_Reader* sectors,
                   unsigned start_sector,
                   unsigned end_sector)
{
    DVDA_Packet_Reader* packets = malloc(sizeof(DVDA_Packet_Reader));
    assert(end_sector >= start_sector);

    packets->start_sector = start_sector;
    packets->end_sector = end_sector;

    packets->sectors = sectors;
    packets->reader = br_substream_new(BS_BIG_ENDIAN);

    packets->total_sectors = end_sector - start_sector;
    seek_sector(sectors, start_sector);
    return packets;
}
Ejemplo n.º 5
0
static int
read_sector(DVDA_Sector_Reader* reader,
            struct bs_buffer* sector)
{
    if (reader->current.sector <= reader->end_sector) {
        DVDA_AOB* aob = reader->current.aob;
        static uint8_t sector_data[SECTOR_SIZE];
        const size_t bytes_read = fread(sector_data,
                                        sizeof(uint8_t),
                                        SECTOR_SIZE,
                                        aob->file);
        buf_write(sector, sector_data, (uint32_t)bytes_read);

        if (bytes_read == SECTOR_SIZE) {
            /*sector read successfully*/

#ifdef HAS_UNPROT
            /*unprotect if necessary*/
            if (reader->cppm_decoder != NULL) {
                cppm_decrypt(reader->cppm_decoder,
                             sector_data, 1, 1);
            }
#endif

            /*then move on to next sector*/
            reader->current.sector++;
            if (reader->current.sector > aob->end_sector) {
                /*move on to next AOB in set, if any*/
                if (reader->current.sector <= reader->end_sector) {
                    seek_sector(reader, reader->current.sector);
                }
            }
            return 0;
        } else {
            /*I/O error reading sector*/
            return 1;
        }
    } else {
        /*no more sectors to read, so return EOF*/
        return 0;
    }
}
Ejemplo n.º 6
0
void format_sector(const unsigned int cylinder, 
		   const unsigned int sector, 
		   const unsigned int nsector, 
		   const unsigned int value){
  
  if( !seek_sector(cylinder, sector) ){
    return;
  }

  _out(HDA_DATAREGS, (nsector>>8 & 0xFF) );
  _out(HDA_DATAREGS+1, (nsector & 0xFF) );
  _out(HDA_DATAREGS+2, (value>>24 & 0xFF) );
  _out(HDA_DATAREGS+3, (value>>16 & 0xFF) );
  _out(HDA_DATAREGS+4, (value>>8 & 0xFF) );
  _out(HDA_DATAREGS+5, (value & 0xFF) );

  _out(HDA_CMDREG, CMD_FORMAT);
  _sleep(HDA_IRQ);

}
Ejemplo n.º 7
0
/*
 * Read a xbsd_disklabel from sector 0 or from the starting sector of p.
 * If it has the right magic, return 1.
 */
static int
xbsd_readlabel(struct partition *p)
{
    struct xbsd_disklabel *d;
    int t, sector;

    if (!bsd_globals_ptr)
        bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));

    d = &xbsd_dlabel;

    /* p is used only to get the starting sector */
#if !defined(__alpha__)
    sector = (p ? get_start_sect(p) : 0);
#else
    sector = 0;
#endif

    seek_sector(sector);
    if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
        fdisk_fatal(unable_to_read);

    memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
            sizeof(struct xbsd_disklabel));

    if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
        return 0;

    for (t = d->d_npartitions; t < BSD_MAXPARTITIONS; t++) {
        d->d_partitions[t].p_size   = 0;
        d->d_partitions[t].p_offset = 0;
        d->d_partitions[t].p_fstype = BSD_FS_UNUSED;
    }

    if (d->d_npartitions > BSD_MAXPARTITIONS)
        printf("Warning: too many partitions (%u, maximum is %u)\n",
               d->d_npartitions, BSD_MAXPARTITIONS);
    return 1;
}
Ejemplo n.º 8
0
void write_sector(unsigned int cylinder, unsigned int sector, unsigned char* buffer){
    
  int i;
  int length;  

  if( !seek_sector(cylinder, sector) ){
    buffer = NULL;
    return;
  }

  /* ecrire les donnees dans MASTERBUFFER */
  length = strlen((char*)buffer);

  for(i=0; i<length || i< HDA_SECTORSIZE; i++){
    MASTERBUFFER[i] = buffer[i];
  }

  /* Mettre dans le disque les donnees de MASTERBUFFER */
  _out(HDA_DATAREGS, 0);
  _out(HDA_DATAREGS, 1);
  _out(HDA_CMDREG, CMD_WRITE);
  _sleep(HDA_IRQ);
}
Ejemplo n.º 9
0
void write_sector_n(const unsigned int cylinder, 
		    const unsigned int sector, 
		    const unsigned char* buffer,
		    const size_t n){
  
  int i;

  if( !seek_sector(cylinder, sector) ){
    buffer = NULL;
    return;
  }

  /* ecrire les donnees dans MASTERBUFFER */
  for(i=0; i<HDA_SECTORSIZE && i<n; i++){
    MASTERBUFFER[i] = buffer[i];
  }

  /* Mettre dans le disque les donnees de MASTERBUFFER */
  _out(HDA_DATAREGS, 0);
  _out(HDA_DATAREGS, 1);
  _out(HDA_CMDREG, CMD_WRITE);
  _sleep(HDA_IRQ);

}
Ejemplo n.º 10
0
static void
xbsd_write_bootstrap(void)
{
    char path[MAXPATHLEN];
    const char *bootdir = BSD_LINUX_BOOTDIR;
    const char *dkbasename;
    struct xbsd_disklabel dl;
    char *d, *p, *e;
    int sector;

    if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)
        dkbasename = "sd";
    else
        dkbasename = "wd";

    snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
             dkbasename, dkbasename, dkbasename);
    if (read_line(path)) {
        dkbasename = line_ptr;
    }
    snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
    if (!xbsd_get_bootstrap(path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))
        return;

    /* We need a backup of the disklabel (xbsd_dlabel might have changed). */
    d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];
    memmove(&dl, d, sizeof(struct xbsd_disklabel));

    /* The disklabel will be overwritten by 0's from bootxx anyway */
    memset(d, 0, sizeof(struct xbsd_disklabel));

    snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
    if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
                            (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
        return;

    e = d + sizeof(struct xbsd_disklabel);
    for (p = d; p < e; p++)
        if (*p) {
            printf("Bootstrap overlaps with disk label!\n");
            exit(EXIT_FAILURE);
        }

    memmove(d, &dl, sizeof(struct xbsd_disklabel));

#if defined(__powerpc__) || defined(__hppa__)
    sector = 0;
#elif defined(__alpha__)
    sector = 0;
    alpha_bootblock_checksum(disklabelbuffer);
#else
    sector = get_start_sect(xbsd_part);
#endif

    seek_sector(sector);
    xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);

#if defined(__alpha__)
    printf("Bootstrap installed on %s\n", disk_device);
#else
    printf("Bootstrap installed on %s\n",
           partname(disk_device, xbsd_part_index+1, 0));
#endif

    sync_disks();
}
Ejemplo n.º 11
0
static int
check_gpt_label(void)
{
	struct partition *first = pt_offset(MBRbuffer, 0);
	struct pte pe;
	uint32_t crc;

	/* LBA 0 contains the legacy MBR */

	if (!valid_part_table_flag(MBRbuffer)
	 || first->sys_ind != LEGACY_GPT_TYPE
	) {
		current_label_type = 0;
		return 0;
	}

	/* LBA 1 contains the GPT header */

	read_pte(&pe, 1);
	gpt_hdr = (void *)pe.sectorbuffer;

	if (gpt_hdr->magic != SWAP_LE64(GPT_MAGIC)) {
		current_label_type = 0;
		return 0;
	}

	if (!global_crc32_table) {
		global_crc32_table = crc32_filltable(NULL, 0);
	}

	crc = SWAP_LE32(gpt_hdr->hdr_crc32);
	gpt_hdr->hdr_crc32 = 0;
	if (gpt_crc32(gpt_hdr, SWAP_LE32(gpt_hdr->hdr_size)) != crc) {
		/* FIXME: read the backup table */
		puts("\nwarning: GPT header CRC is invalid\n");
	}

	n_parts = SWAP_LE32(gpt_hdr->n_parts);
	part_entry_len = SWAP_LE32(gpt_hdr->part_entry_len);
	if (n_parts > GPT_MAX_PARTS
	 || part_entry_len > GPT_MAX_PART_ENTRY_LEN
	 || SWAP_LE32(gpt_hdr->hdr_size) > sector_size
	) {
		puts("\nwarning: unable to parse GPT disklabel\n");
		current_label_type = 0;
		return 0;
	}

	part_array_len = n_parts * part_entry_len;
	part_array = xmalloc(part_array_len);
	seek_sector(SWAP_LE64(gpt_hdr->first_part_lba));
	if (full_read(dev_fd, part_array, part_array_len) != part_array_len) {
		fdisk_fatal(unable_to_read);
	}

	if (gpt_crc32(part_array, part_array_len) != gpt_hdr->part_array_crc32) {
		/* FIXME: read the backup table */
		puts("\nwarning: GPT array CRC is invalid\n");
	}

	puts("Found valid GPT with protective MBR; using GPT\n");

	current_label_type = LABEL_GPT;
	return 1;
}