Beispiel #1
0
void* main(void)
{
    char filename[MAX_PATH];
    int i;
    int btn;
    int rc;
    int num_partitions;
    struct partinfo* pinfo;
#if !(CONFIG_STORAGE & STORAGE_SD)
    char buf[256];
    unsigned short* identify_info;
#endif
    int usb = USB_EXTRACTED;

    chksum_crc32gentab ();

    system_init();
    kernel_init();

#ifdef HAVE_BOOTLOADER_USB_MODE
    /* loader must service interrupts */
    enable_interrupt(IRQ_FIQ_STATUS);
#endif

    lcd_init();

    font_init();
    show_logo();

    adc_init();
#ifdef HAVE_BOOTLOADER_USB_MODE
    button_init_device();
#else
    button_init();
#endif
#if defined(SANSA_E200) || defined(PHILIPS_SA9200)
    i2c_init();
    _backlight_on();
#endif

    if (button_hold())
    {
        verbose = true;
        lcd_clear_display();
        printf("Hold switch on");
        printf("Shutting down...");
        sleep(HZ);
        power_off();
    }

    btn = button_read_device();

    /* Enable bootloader messages if any button is pressed */
#ifdef HAVE_BOOTLOADER_USB_MODE
    lcd_clear_display();
    if (btn)
        verbose = true;
#else
    if (btn) {
        lcd_clear_display();
        verbose = true;
    }
#endif

    lcd_setfont(FONT_SYSFIXED);

    printf("Rockbox boot loader");
    printf("Version: " RBVERSION);
    printf(MODEL_NAME);

    i=storage_init();
#if !(CONFIG_STORAGE & STORAGE_SD)
    if (i==0) {
        identify_info=ata_get_identify();
        /* Show model */
        for (i=0; i < 20; i++) {
            ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
        }
        buf[40]=0;
        for (i=39; i && buf[i]==' '; i--) {
            buf[i]=0;
        }
        printf(buf);
    } else {
        error(EATA, i, true);
    }
#endif

    disk_init(IF_MV(0));
    num_partitions = disk_mount_all();
    if (num_partitions<=0)
    {
        error(EDISK,num_partitions, true);
    }

    /* Just list the first 2 partitions since we don't have any devices yet 
       that have more than that */
    for(i=0; i<NUM_PARTITIONS; i++)
    {
        pinfo = disk_partinfo(i);
        printf("Partition %d: 0x%02x %ld MB",
                i, pinfo->type, pinfo->size / 2048);
    }

    /* Now that storage is initialized, check for USB connection */
    if ((btn & BOOTLOADER_BOOT_OF) == 0)
    {
        usb_pin_init();
        usb = handle_usb(HZ*2);
        if (usb == USB_INSERTED)
            btn |= BOOTLOADER_BOOT_OF;
    }

    /* Try loading Rockbox, if that fails, fall back to the OF */
    if((btn & BOOTLOADER_BOOT_OF) == 0)
    {
        printf("Loading Rockbox...");
        snprintf(filename,sizeof(filename), BOOTDIR "/%s", BOOTFILE);

        rc = load_mi4(loadbuffer, filename, MAX_LOADSIZE);
        if (rc <= EFILE_EMPTY)
        {
            bool old_verbose = verbose;
            verbose = true;
            printf("Can't load " BOOTFILE ": ");
            printf(loader_strerror(rc));
            verbose = old_verbose;
            btn |= BOOTLOADER_BOOT_OF;
            sleep(5*HZ);
        }
        else
            goto main_exit;
    }

    if(btn & BOOTLOADER_BOOT_OF)
    {
        /* Load original mi4 firmware in to a memory buffer called loadbuffer.
           The rest of the loading is done in crt0.S.
           1) First try reading from the hidden partition (on Sansa only).
           2) Next try a decrypted mi4 file in /System/OF.mi4
           3) Finally, try a raw firmware binary in /System/OF.bin. It should be
              a mi4 firmware decrypted and header stripped using mi4code.
        */
        printf("Loading original firmware...");

#if (CONFIG_STORAGE & STORAGE_SD)
        /* First try a (hidden) firmware partition */
        printf("Trying firmware partition");
        pinfo = disk_partinfo(1);
        if(pinfo->type == PARTITION_TYPE_OS2_HIDDEN_C_DRIVE)
        {
            rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE,
                               usb == USB_INSERTED);
            if (rc <= EFILE_EMPTY) {
                printf("Can't load from partition");
                printf(loader_strerror(rc));
            } else {
                goto main_exit;
            }
        } else {
            printf("No hidden partition found.");
        }
#endif

#if defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) || defined(PHILIPS_SA9200)
        printf("Trying /System/OF.ebn");
        rc=load_mi4(loadbuffer, "/System/OF.ebn", MAX_LOADSIZE);
        if (rc <= EFILE_EMPTY) {
            printf("Can't load /System/OF.ebn");
            printf(loader_strerror(rc));
        } else {
            goto main_exit;
        }
#endif

        printf("Trying /System/OF.mi4");
        rc=load_mi4(loadbuffer, "/System/OF.mi4", MAX_LOADSIZE);
        if (rc <= EFILE_EMPTY) {
            printf("Can't load /System/OF.mi4");
            printf(loader_strerror(rc));
        } else {
#if defined(SAMSUNG_YH925)
            lcd_reset();
#endif
            goto main_exit;
        }

        printf("Trying /System/OF.bin");
        rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE);
        if (rc <= EFILE_EMPTY) {
            printf("Can't load /System/OF.bin");
            printf(loader_strerror(rc));
        } else {
#if defined(SAMSUNG_YH925)
            lcd_reset();
#endif
            goto main_exit;
        }
        
        error(0, 0, true);
    }

main_exit:
#ifdef HAVE_BOOTLOADER_USB_MODE
    storage_close();
    system_prepare_fw_start();
#endif

    return (void*)loadbuffer;
}
void main(uint32_t arg, uint32_t addr)
{
    unsigned char* loadbuffer;
    int buffer_size;
    void(*kernel_entry)(void);
    int ret;

    system_init();
    kernel_init();

    power_init();
    enable_irq();

    lcd_init();
    lcd_clear_display();
    lcd_update();

    backlight_init();

    button_init();

    //button_debug_screen();
    printf("Boot version: %s", RBVERSION);
    printf("arg=%x addr=%x", arg, addr);

#ifdef SANSA_FUZEPLUS
    extern void imx233_mmc_disable_window(void);
    if(arg == 0xfee1dead)
    {
        printf("Disable MMC window.");
        imx233_mmc_disable_window();
    }
#endif

    ret = storage_init();
    if(ret < 0)
        error(EATA, ret, true);

    /* NOTE: allow disk_init and disk_mount_all to fail since we can do USB after.
     * We need this order to determine the correct logical sector size */
    while(!disk_init(IF_MV(0)))
        printf("disk_init failed!");

    if((ret = disk_mount_all()) <= 0)
        error(EDISK, ret, false);

    if(usb_detect() == USB_INSERTED)
        usb_mode(HZ);

    printf("Loading firmware");

    loadbuffer = (unsigned char*)loadaddress;
    buffer_size = (int)(loadaddressend - loadaddress);

    while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) < 0)
    {
        error(EBOOTFILE, ret, true);
    }

    kernel_entry = (void*) loadbuffer;
    printf("Executing");
    disable_interrupt(IRQ_FIQ_STATUS);
    commit_discard_idcache();
    kernel_entry();
    printf("ERR: Failed to boot");

    /* never returns */
    while(1) ;
}