Example #1
0
void mbr_startup(void )
{
#ifndef BOOTLOADER_SUPPORT
  mbr_config.success = 1;
#else
  mbr_config.success = 0;
#endif
  mbr_config.bootloader = 0;
  write_mbr();
}
Example #2
0
/*
 * hook called before writing new disklabel.
 */
int
md_pre_disklabel(void)
{
	msg_display(MSG_dofdisk);

	/* write edited MBR onto disk. */
	if (write_mbr(diskdev, &mbr, 1) != 0) {
		msg_display(MSG_wmbrfail);
		process_menu(MENU_ok, NULL);
		return 1;
	}
	return 0;
}
Example #3
0
int edit_record(char *device_name, uint64_t offset) {
    if (offset == 0) {
        mbr_t mbr;

        read_mbr(device_name, &mbr);

        if (OKAY == edit_mbr_loop(&mbr) && are_you_sure() == YES) {
            write_mbr(device_name, &mbr);
        }
    }
    else {
        ebr_t ebr;

        read_ebr(device_name, offset, &ebr);

        if (OKAY == edit_ebr_loop(&ebr) && are_you_sure() == YES) {
            write_ebr(device_name, offset, &ebr);
        }
    }
    return OKAY;
} 
Example #4
0
UINTN gptsync(VOID)
{
    UINTN   status = 0;
    UINTN   status_gpt, status_mbr;
    BOOLEAN proceed = FALSE;

    Print(L"gptsync version %s\ncopyright (c) 2006-2007 Christoph Pfisterer & 2013 Roderick W. Smith\n", VERSION);

    // get full information from disk
    status_gpt = read_gpt();
    status_mbr = read_mbr();
    if (status_gpt != 0 || status_mbr != 0)
        return (status_gpt || status_mbr);

    // cross-check current situation
    Print(L"\n");
    status = check_gpt();   // check GPT for consistency
    if (status != 0)
        return status;
    status = check_mbr();   // check MBR for consistency
    if (status != 0)
        return status;
    status = analyze();     // analyze the situation & compose new MBR table
    if (status != 0)
        return status;
    if (new_mbr_part_count == 0)
        return status;

    // offer user the choice what to do
    status = input_boolean(STR("\nMay I update the MBR as printed above? [y/N] "), &proceed);
    if (status != 0 || proceed != TRUE)
        return status;

    // adjust the MBR and write it back
    status = write_mbr();
    if (status != 0)
        return status;

    return status;
}
Example #5
0
File: mbr.c Project: Levrifon/ASE
/* renvoie si le disque est déjà partitionné ou non */
int read_mbr(void) {
        //printf("debug mbr \n");
	char buffer[HDA_SECTORSIZE];
	read_sectorn(0,0,buffer,sizeof(struct mbr_s)); /* on lit au tout debut du secteur */
	memcpy(&mbr,buffer,sizeof(struct mbr_s)); /*on copie le resultat dans le mbr */
	//printf("read_mbr : %d == %d\n", mbr.mbr_magic, MBR_MAGIC);
	if(mbr.mbr_magic != MBR_MAGIC) {
	        printf("init mbr\n");
		mbr.mbr_magic = MBR_MAGIC;
		mbr.mbr_n_vol = 0;
		int i;
                for(i=0;i<MAX_VOL;i++) {
                        mbr.mbr_vol[i].vol_first_cylinder=0;
                        mbr.mbr_vol[i].vol_first_sector=0;
                        mbr.mbr_vol[i].vol_nblocs=1;
                        mbr.mbr_vol[i].vol_type=VOL_UNUSED;
                }
                write_mbr();
		return 0;
	}
	return 1;
}
Example #6
0
static int dos_set_disk_signature(struct param_d *p, void *_priv)
{
	struct disk_signature_priv *priv = _priv;
	struct block_device *blk = priv->blk;
	void *buf;
	__le32 *disksigp;
	int ret;

	buf = read_mbr(blk);
	if (!buf)
		return -EIO;

	disksigp = buf + 0x1b8;

	*disksigp = cpu_to_le32(priv->signature);

	ret = write_mbr(blk, buf);

	free(buf);

	return ret;
}
Example #7
0
UINTN gptsync(int optind, int argc, char **argv)
{
    UINTN   status = 0;
    UINTN   status_gpt, status_mbr;
    BOOLEAN proceed = FALSE;
    
    // get full information from disk
    status_gpt = read_gpt();
    status_mbr = read_mbr();
    if (status_gpt != 0 || status_mbr != 0)
        return (status_gpt || status_mbr);
    
    // cross-check current situation
    Print(L"\n");
    status = check_gpt();   // check GPT for consistency
    if (status != 0)
        return status;
    status = check_mbr();   // check MBR for consistency
    if (status != 0)
        return status;
    status = analyze(optind, argc, argv);     // analyze the situation & compose new MBR table
    if (status != 0)
        return status;

    // offer user the choice what to do
    status = input_boolean(STR("\nMay I update the MBR as printed above? [y/N] "), &proceed);
    if (status != 0 || proceed != TRUE)
        return status;
    
    // adjust the MBR and write it back
    status = write_mbr();
    if (status != 0)
        return status;
    
    return status;
}
Example #8
0
/*
 * Boot manager installation/configuration utility.
 */
int
main(int argc, char *argv[])
{
    u_int8_t *mbr, *boot0;
    int boot0_size, mbr_size;
    const char *bpath, *fpath;
    char *disk;
    int B_flag, o_flag;
    int d_arg, m_arg, s_arg, t_arg;
    int o_and, o_or, o_e = -1;
    int up, c;

    bpath = "/boot/boot0";
    fpath = NULL;
    B_flag = v_flag = o_flag = 0;
    d_arg = m_arg = s_arg = t_arg = -1;
    o_and = 0xff;
    o_or = 0;
    while ((c = getopt(argc, argv, "Bvb:d:e:f:i:m:o:s:t:")) != -1)
        switch (c) {
        case 'B':
            B_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'b':
            bpath = optarg;
            break;
        case 'd':
            d_arg = argtoi(optarg, 0, 0xff, 'd');
            break;
        case 'e':
	    if (optarg[0] == '0' && optarg[1] == 'x')
		sscanf(optarg, "0x%02x", &o_e);
	    else
		o_e = optarg[0];
            break;
        case 'f':
            fpath = optarg;
            break;
        case 'i':
            if (sscanf(optarg, "%02x%02x-%02x%02x",
		vol_id, vol_id+1, vol_id+2, vol_id+3) == 4)
			vol_id[4] = 1;
	    else
		errx(1, "bad argument %s", optarg);
            break;
        case 'm':
            m_arg = argtoi(optarg, 0, 0xf, 'm');
            break;
        case 'o':
            stropt(optarg, &o_and, &o_or);
            o_flag = 1;
            break;
        case 's':
	    if (strcasecmp(optarg, "pxe") == 0)
		s_arg = 6;
	    else
		s_arg = argtoi(optarg, 1, 6, 's');
            break;
        case 't':
            t_arg = argtoi(optarg, 1, 0xffff, 't');
            break;
        default:
            usage();
        }
    argc -= optind;
    argv += optind;
    if (argc != 1)
        usage();
    disk = g_device_path(*argv);
    if (disk == NULL)
        errx(1, "Unable to get providername for %s\n", *argv);
    up = B_flag || d_arg != -1 || m_arg != -1 || o_flag || s_arg != -1
	|| t_arg != -1;

    /* open the disk and read in the existing mbr. Either here or
     * when reading the block from disk, we do check for the version
     * and abort if a suitable block is not found.
     */
    mbr_size = read_mbr(disk, &mbr, !B_flag);

    /* save the existing MBR if we are asked to do so */
    if (fpath)
	write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size);

    /*
     * If we are installing the boot loader, read it from disk and copy the
     * slice table over from the existing MBR.  If not, then point boot0
     * back at the MBR we just read in.  After this, boot0 is the data to
     * write back to disk if we are going to do a write.
     */
    if (B_flag) {
	boot0_size = read_mbr(bpath, &boot0, 1);
        memcpy(boot0 + OFF_PTBL, mbr + OFF_PTBL,
	    sizeof(struct dos_partition) * NDOSPART);
	if (b0_ver == 2)	/* volume serial number support */
	    memcpy(boot0 + OFF_SERIAL, mbr + OFF_SERIAL, 4);
    } else {
	boot0 = mbr;
	boot0_size = mbr_size;
    }

    /* set the drive */
    if (d_arg != -1)
	boot0[OFF_DRIVE] = d_arg;

    /* set various flags */
    if (m_arg != -1) {
	boot0[OFF_FLAGS] &= 0xf0;
	boot0[OFF_FLAGS] |= m_arg;
    }
    if (o_flag) {
        boot0[OFF_FLAGS] &= o_and;
        boot0[OFF_FLAGS] |= o_or;
    }

    /* set the default boot selection */
    if (s_arg != -1)
        boot0[OFF_OPT] = s_arg - 1;

    /* set the timeout */
    if (t_arg != -1)
        mk2(boot0 + OFF_TICKS, t_arg);

    /* set the bell char */
    if (o_e != -1 && set_bell(boot0, o_e, 0) != -1)
	up = 1;

    if (vol_id[4]) {
	if (b0_ver != 2)
	    errx(1, "incompatible boot block, cannot set volume ID");
	boot0[OFF_SERIAL] = vol_id[0];
	boot0[OFF_SERIAL+1] = vol_id[1];
	boot0[OFF_SERIAL+2] = vol_id[2];
	boot0[OFF_SERIAL+3] = vol_id[3];
	up = 1;	/* force update */
    }
    /* write the MBR back to disk */
    if (up)
	write_mbr(disk, 0, boot0, boot0_size);

    /* display the MBR */
    if (v_flag)
	display_mbr(boot0);

    /* clean up */
    if (mbr != boot0)
	free(boot0);
    free(mbr);
    free(disk);

    return 0;
}
Example #9
0
int
main (void)
{
#ifdef BOOTLOADER_SUPPORT
  _IVREG = _BV (IVCE);    /* prepare ivec change */
  _IVREG = _BV (IVSEL);   /* change ivec to bootloader */
#endif

  /* Default DDR Config */
#if IO_HARD_PORTS >= 4 && DDR_MASK_A != 0
  DDRA = DDR_MASK_A;
#endif
#if DDR_MASK_B != 0
  DDRB = DDR_MASK_B;
#endif
#if DDR_MASK_C != 0
  DDRC = DDR_MASK_C;
#endif
#if DDR_MASK_D != 0
  DDRD = DDR_MASK_D;
#endif
#if IO_HARD_PORTS >= 6
#if DDR_MASK_E != 0
  DDRE = DDR_MASK_E;
#endif
#if DDR_MASK_F != 0
  DDRF = DDR_MASK_F;
#endif
#endif
#if IO_HARD_PORTS >= 7
#if DDR_MASK_G != 0
  DDRG = DDR_MASK_G;
#endif
#endif


#ifdef STATUSLED_POWER_SUPPORT
  PIN_SET(STATUSLED_POWER);
#endif

  //FIXME: zum ethersex meta system hinzufügen, aber vor allem anderem initalisieren
  debug_init();
  debug_printf("%S (Debug mode)\n", pstr_E6_VERSION_STRING_LONG);

#ifdef DEBUG_RESET_REASON
  if (bit_is_set (mcusr_mirror, BORF))
    debug_printf("reset: Brown-out\n");
  else if (bit_is_set (mcusr_mirror, PORF))
    debug_printf("reset: Power on\n");
  else if (bit_is_set (mcusr_mirror, WDRF))
    debug_printf("reset: Watchdog\n");
  else if (bit_is_set (mcusr_mirror, EXTRF))
    debug_printf("reset: Extern\n");
  else
    debug_printf("reset: Unknown\n");
#endif

#ifdef BOOTLOADER_SUPPORT
  /* disable interrupts */
  cli ();
  wdt_disable();
#endif //BOOTLOADER_SUPPORT
  /* enable interrupts */
  sei ();

#ifdef USE_WATCHDOG
  debug_printf("enabling watchdog\n");
#ifdef DEBUG
  /* for debugging, test reset cause and jump to bootloader */
  if (MCU_STATUS_REGISTER & _BV (WDRF))
  {
    debug_printf("bootloader...\n");
    jump_to_bootloader();
  }
#endif
  /* set watchdog to 2 seconds */
  wdt_enable(WDTO_2S);
  wdt_kick();
#else //USE_WATCHDOG
  debug_printf("disabling watchdog\n");
  wdt_disable();
#endif //USE_WATCHDOG

#ifdef SPI_SUPPORT
  spi_init();
#endif

  ethersex_meta_init();

  /* must be called AFTER all other initialization */
#ifdef PORTIO_SUPPORT
  portio_init();
#elif defined(NAMED_PIN_SUPPORT)
  np_simple_init();
#endif

#ifdef ENC28J60_SUPPORT
  debug_printf ("enc28j60 revision 0x%x\n",
  read_control_register (REG_EREVID));
  debug_printf ("mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
	  uip_ethaddr.addr[0], uip_ethaddr.addr[1], uip_ethaddr.addr[2],
	  uip_ethaddr.addr[3], uip_ethaddr.addr[4], uip_ethaddr.addr[5]);
#endif

#ifdef STATUSLED_BOOTED_SUPPORT
  PIN_SET(STATUSLED_BOOTED);
#endif

  ethersex_meta_startup();
  /* main loop */
  while (1)
  {
    wdt_kick();
    ethersex_meta_mainloop();

#ifdef SD_READER_SUPPORT
    if (sd_active_partition == NULL)
    {
      if (!sd_try_init())
      {
#ifdef VFS_SD_SUPPORT
        vfs_sd_try_open_rootnode();
#endif
      }
      wdt_kick();
    }
#endif

#ifdef BOOTLOADER_JUMP
    if (status.request_bootloader)
    {
#ifdef MBR_SUPPORT
      mbr_config.bootloader = 1;
      write_mbr();
#endif
#ifdef CLOCK_CRYSTAL_SUPPORT
      TIMER_8_AS_1_INT_OVERFLOW_OFF;
#endif
#ifdef DCF77_SUPPORT
      ACSR &= ~_BV (ACIE);
#endif
      cli();
      jump_to_bootloader();
    }
#endif

#ifndef TEENSY_SUPPORT
    if (status.request_wdreset)
    {
      cli();
      wdt_enable(WDTO_15MS);
      for (;;);
    }
#endif

    if (status.request_reset)
    {
      cli();
      void (*reset) (void) = NULL;
      reset();
    }
  }
}
Example #10
0
int write_mbr_and_reboot(char * filename)
{
  char * new_mbr;
  char nyancat[512] = {
    0x0e, 0x1f, 0x0e, 0x07, 0xfc, 0xb9, 0x14, 0x00, 0xbf, 0xbb, 0x7d, 0x0f,
    0x31, 0x96, 0x31, 0xf0, 0xc1, 0xc6, 0x07, 0xab, 0xe2, 0xf5, 0xb8, 0x13,
    0x00, 0xcd, 0x10, 0x68, 0x00, 0xa0, 0x07, 0x6a, 0x04, 0x6a, 0x00, 0xbd,
    0x80, 0x02, 0x31, 0xff, 0xb8, 0x7e, 0x00, 0x31, 0xc9, 0x49, 0xf3, 0xaa,
    0x68, 0x00, 0x50, 0x5f, 0xb1, 0x05, 0x57, 0x01, 0xef, 0xf7, 0xdd, 0xe8,
    0x9c, 0x00, 0x5f, 0x83, 0xc7, 0x18, 0xe2, 0xf2, 0x01, 0xef, 0x68, 0x1b,
    0x7d, 0x5e, 0xe8, 0xa5, 0x00, 0xe8, 0x9f, 0x00, 0x58, 0x5a, 0xa8, 0x01,
    0x74, 0x04, 0xf7, 0xdd, 0xf7, 0xd2, 0xf7, 0xdd, 0x40, 0x52, 0x50, 0x29,
    0xd7, 0xe8, 0x8b, 0x00, 0xb1, 0x05, 0xe8, 0x4a, 0x00, 0xe2, 0xfb, 0x81,
    0xc7, 0xb4, 0x13, 0xe8, 0xa1, 0x00, 0x83, 0xc7, 0x14, 0xe8, 0x9b, 0x00,
    0x83, 0xc7, 0x24, 0xe8, 0x95, 0x00, 0x83, 0xc7, 0x18, 0xe8, 0x8f, 0x00,
    0xb1, 0x14, 0xbb, 0xbb, 0x7d, 0x8b, 0x3f, 0x58, 0x50, 0xc1, 0xe0, 0x04,
    0x29, 0xc7, 0x31, 0xc0, 0x81, 0xff, 0x00, 0xe6, 0x77, 0x08, 0xbe, 0x9f,
    0x7d, 0x53, 0xe8, 0x12, 0x00, 0x5b, 0x83, 0xc3, 0x02, 0xe2, 0xe2, 0x31,
    0xc0, 0x99, 0xb1, 0x02, 0xb4, 0x86, 0xcd, 0x15, 0xe9, 0x73, 0xff, 0x31,
    0xdb, 0xac, 0x93, 0xac, 0x92, 0xad, 0x92, 0x83, 0xfa, 0x01, 0x74, 0x09,
    0x01, 0xd7, 0x89, 0xda, 0xe8, 0x03, 0x00, 0xeb, 0xef, 0xc3, 0x51, 0x57,
    0x89, 0xd9, 0xf3, 0xaa, 0x5f, 0x81, 0xc7, 0x40, 0x01, 0x4a, 0x75, 0xf3,
    0x59, 0xc3, 0x51, 0xb1, 0x05, 0x6a, 0x28, 0x58, 0x6a, 0x18, 0x5b, 0x6a,
    0x0c, 0x5a, 0xe8, 0xe1, 0xff, 0x04, 0x04, 0xe2, 0xf3, 0x59, 0xc3, 0xe8,
    0x00, 0x00, 0xad, 0x01, 0xc7, 0x31, 0xc0, 0xac, 0x91, 0xac, 0x93, 0xac,
    0x99, 0x92, 0xac, 0x57, 0x52, 0xe8, 0xc6, 0xff, 0x5a, 0x5f, 0x80, 0xea,
    0x08, 0x80, 0xc3, 0x08, 0x81, 0xc7, 0xfc, 0x04, 0xe2, 0xed, 0xc3, 0x57,
    0x56, 0xe8, 0xd7, 0xff, 0x5e, 0x5f, 0xc3, 0x00, 0xfb, 0x03, 0x48, 0x48,
    0x00, 0x0c, 0xf6, 0x02, 0x48, 0x40, 0x59, 0x10, 0xfb, 0x03, 0x38, 0x38,
    0x3c, 0x38, 0x0f, 0x04, 0x28, 0x28, 0x00, 0x10, 0xf1, 0x03, 0x28, 0x20,
    0x19, 0x10, 0x19, 0x04, 0xe2, 0x28, 0xec, 0x01, 0x00, 0x08, 0x00, 0xe4,
    0x04, 0x1c, 0xf6, 0x01, 0x00, 0x04, 0x0f, 0xe4, 0xf5, 0x1c, 0xfb, 0x01,
    0x00, 0x08, 0x41, 0xdc, 0x04, 0x2c, 0xf6, 0x01, 0x00, 0x04, 0x00, 0xd0,
    0xe1, 0x00, 0xf6, 0x00, 0xf6, 0x00, 0xf6, 0x04, 0xf6, 0x04, 0xfb, 0x04,
    0x00, 0x00, 0x00, 0x1c, 0xfb, 0x00, 0xf6, 0x04, 0xf6, 0x04, 0xfb, 0x04,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdc, 0x13, 0x00, 0x00, 0x04,
    0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x00,
    0xf6, 0xf4, 0xfa, 0x04, 0xf1, 0x01, 0x00, 0x00, 0x00, 0x02, 0x08, 0x0c,
    0x00, 0x08, 0xfb, 0x01, 0x08, 0x04, 0x19, 0x04, 0x0f, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xec, 0x04, 0xfb, 0x04, 0x00, 0x00,
    0x00, 0xf4, 0xf5, 0x00, 0x00, 0x04, 0x00, 0x04, 0xfb, 0x01, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
  };
  int size;

  if(!filename)
  {
    new_mbr = nyancat;
    size = sizeof(nyancat);
    printf("Using default mbr (nyancat)\n");
  }
  else
  {
    new_mbr = get_file_contents(filename, &size);
    if(!new_mbr)
      return 1;
  }
  if(write_mbr(new_mbr, size))
    return 1;
  return reboot_system();
}