static void cmd_erase(struct protocol_handle *phandle, const char *arg)
{
    int partition_fd;
    char path[PATH_MAX];
    D(DEBUG, "cmd_erase %s\n", arg);

    if (flash_find_entry(arg, path, PATH_MAX)) {
        fastboot_fail(phandle, "partition table doesn't exist");
        return;
    }

    if (path == NULL) {
        fastboot_fail(phandle, "Couldn't find partition");
        return;
    }

    partition_fd = flash_get_partiton(path);
    if (partition_fd < 0) {
        fastboot_fail(phandle, "partiton file does not exists");
    }

    if (flash_erase(partition_fd)) {
        fastboot_fail(phandle, "failed to erase partition");
        flash_close(partition_fd);
        return;
    }

    if (flash_close(partition_fd) < 0) {
        D(ERR, "could not close device %s", strerror(errno));
        fastboot_fail(phandle, "failed to erase partition");
        return;
    }
    fastboot_okay(phandle, "");
}
Example #2
0
int
fs_device_ftl_flash_close (efs_device_t base_dev)
{
  int result = -1;

  result = flash_close (base_dev->priv.ftl_flash);
  base_dev->priv.ftl_flash = NULL;
  return result;
}
Example #3
0
static void
m128rfa1_deinit(avr_t *avr, void *data)
{
    (void)data;

    uart_pty_stop(&uart_pty[0]);
    uart_pty_stop(&uart_pty[1]);

    flash_close(avr->flash, avr->flashend + 1);
    avr->flash = NULL;
}
Example #4
0
void emu_cleanup()
{
    exiting = true;

    if(debugger_input)
        fclose(debugger_input);

    // addr_cache_init is rather expensive and needs to be called once only
    //addr_cache_deinit();

    #ifndef NO_TRANSLATION
        translate_deinit();
    #endif

    memory_reset();
    memory_deinitialize();
    flash_close();

    gdbstub_quit();
    rdebug_quit();
}
Example #5
0
int update_fw(const Value *fw_file, const Value *ec_file, int force)
{
	int res = -EINVAL;
	struct flash_device *img, *spi, *ec;
	size_t size;
	char *fwid;
	char cur_part = vboot_get_mainfw_act();
	char *version = fdt_read_string("firmware-version");
	if (!version) {
		ALOGW("Cannot read firmware version from FDT\n");
		return -EIO;
	}
	ALOGD("Running firmware: %s / partition %c\n", version, cur_part);

	img = flash_open("file", fw_file);
	if (!img)
		goto out_free;
	fwid = reinterpret_cast<char*>(fmap_read_section(img, "RW_FWID_A", &size, NULL));

	if (!fwid) {
		ALOGD("Cannot find firmware image version\n");
		goto out_close_img;
	}

	/* TODO: force update if keyblock does not match */
	if (!strncmp(version, fwid, size) && !force) {
		ALOGI("Firmware already up-to-date: %s\n", version);
		free(fwid);
		res = 0;
		goto out_close_img;
	}
	free(fwid);

	ec = flash_open("ec", NULL);
	if (!ec)
		goto out_close_img;
	spi = flash_open("spi", NULL);
	if (!spi)
		goto out_close_ec;

	if (0)
		res = update_ap_fw(spi, img);

	if (cur_part == 'R') /* Recovery mode */
		res = update_recovery_fw(spi, ec, img, ec_file);
	else /* Normal mode */
		res = update_rw_fw(spi, img, cur_part);

	if (!res) /* successful update : record it */
		res = 1;

	flash_close(spi);
out_close_ec:
	flash_close(ec);
out_close_img:
	flash_close(img);

out_free:
	free(version);
	return res;
}
static void cmd_flash(struct protocol_handle *phandle, const char *arg)
{
    int partition;
    uint64_t sz;
    char data[BOOT_MAGIC_SIZE];
    char path[PATH_MAX];
    ssize_t header_sz = 0;
    int data_fd = 0;

    D(DEBUG, "cmd_flash %s\n", arg);

    if (try_handle_virtual_partition(phandle, arg)) {
        return;
    }

    if (phandle->download_fd < 0) {
        fastboot_fail(phandle, "no kernel file");
        return;
    }

    if (flash_find_entry(arg, path, PATH_MAX)) {
        fastboot_fail(phandle, "partition table doesn't exist");
        return;
    }

    if (flash_validate_certificate(phandle->download_fd, &data_fd) != 1) {
        fastboot_fail(phandle, "Access forbiden you need certificate");
        return;
    }

    // TODO: Maybe its goot idea to check whether the partition is bootable
    if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
        if (read_data_once(data_fd, data, BOOT_MAGIC_SIZE) < BOOT_MAGIC_SIZE) {
            fastboot_fail(phandle, "incoming data read error, cannot read boot header");
            return;
        }
        if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
            fastboot_fail(phandle, "image is not a boot image");
            return;
        }
    }

    partition = flash_get_partiton(path);

    sz = get_file_size64(data_fd);

    sz -= header_sz;

    if (sz > get_file_size64(partition)) {
        flash_close(partition);
        D(WARN, "size of file too large");
        fastboot_fail(phandle, "size of file too large");
        return;
    }

    D(INFO, "writing %"PRId64" bytes to '%s'\n", sz, arg);

    if (flash_write(partition, phandle->download_fd, sz, header_sz)) {
        fastboot_fail(phandle, "flash write failure");
        return;
    }
    D(INFO, "partition '%s' updated\n", arg);

    flash_close(partition);
    close(data_fd);

    fastboot_okay(phandle, "");
}
Example #7
0
/*!
*******************************************************************************
**
** \brief Get the number of flash sectors occupied by the boot image
**
** This function calculates the number of flash sectors occupied by the
** boot image and the leading boot block which contains some size and copy
** information as well as a variable number of register/value settings.
**
** \param device  The type of boot device
** \param sectors The address of a buffer where to write the result into
**
** \return
** - GD_OK if successfull, the sectors argument contains the number of
**         occupied sectors
** - non-GD_OK in case of error, see the GD_FLASH documentation for possible
**             return values
**
*******************************************************************************
*/
GERR GD_SYS_BootImageGetSectors( GD_SYS_BOOT_DEVICE_E device, U16* sectors )
{
    GERR          status;
    GD_HANDLE     handle;
    GD_SYS_BOOT_S bootblock;
    U32           index;
    U16           u16words;
    U32           u32words;
    U32           address;
    U16           sector;
    GERR        (*flash_open)(GD_HANDLE*);
    GERR        (*flash_close)(GD_HANDLE*);
    GERR        (*flash_get_sector)(GD_HANDLE,U32,U16*);
    
#if !defined (SMARTMPEG_D)
    if( device == GD_SYS_BOOT_DEVICE_FLASH )
    {
        flash_open       = GD_FL_Open;
        flash_close      = GD_FL_Close;
        flash_get_sector = GD_FL_GetSector;
    }
    else // device == GD_SYS_BOOT_DEVICE_SFLASH   
#endif
    { 
        #if defined(SMARTMPEG_C) || defined(SMARTMPEG_M) || defined(SMARTMPEG_D) || defined(GK6105S) || defined(GK6106)
            flash_open       = GD_SFLASH_Open;
            flash_close      = GD_SFLASH_Close;
            flash_get_sector = GD_SFLASH_GetSector;  
        #else
            return( GD_ERR_BAD_PARAMETER );
        #endif
    }

    status = flash_open( &handle );
    if( status == GD_OK )
    {
        u16words = sizeof(bootblock) / sizeof(U16);
        u32words = sizeof(bootblock) / sizeof(U32);

        #if !defined(SMARTMPEG_D)
        if( device == GD_SYS_BOOT_DEVICE_FLASH )
        {
            status = GD_FL_Read( handle, 0, (U16*)(void*)&bootblock, u16words );   
        }
        else
        #endif
        {
            #if defined(SMARTMPEG_C) || defined(SMARTMPEG_M) || defined(SMARTMPEG_D) || defined(GK6105S) || defined(GK6106)
                status = GD_SFLASH_Read( handle, 0, (U32*)(void*)&bootblock, u32words );
            #else
                status = GD_ERR_BAD_PARAMETER;
            #endif
        }
        
        //GM_Printf( "bootblock.magic_id = 0x%08x\n", bootblock.magic_id );
        //GM_Printf( "bootblock.image_words = 0x%08x\n", bootblock.image_words );
        //GM_Printf( "bootblock.target_address = 0x%08x\n", bootblock.target_address );
        //GM_Printf( "bootblock.start_address = 0x%08x\n", bootblock.start_address );
        //for( index=0; bootblock.register_defs[index].address != 0x00000000; index++ )
        //{
        //    GM_Printf( "bootblock.register_defs[%d] = 0x%08x:0x%08x\n", index, bootblock.register_defs[index].address, bootblock.register_defs[index].data  );
        //}

        if( status == GD_OK )
        {    
            if( ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG    )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_E1 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_E2 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_L1 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_L2 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_C1 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_C2 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_C4 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_M1 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_M2 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_M4 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_G1 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_G2 )
            ||  ( bootblock.magic_id == GD_SYS_MAGIC_ID_SMARTMPEG_G4 ) )
            {
                // search for the termination of register/value pairs             
                for( index=0; bootblock.register_defs[index].address != 0x00000000; index++ )
                    ;
                u32words = 4 + ( index * ( sizeof(GD_SYS_BOOT_REG_S)/sizeof(U32) ) ) + 1;
            
                // calculate the required size in bytes for the
                // boot image plus the leading boot block information 
                // containing the register/value pair settings            

                u32words += bootblock.image_words;
                
                #if defined(SMARTMPEG_C) || defined(SMARTMPEG_M) || defined(SMARTMPEG_D) || defined(GK6105S) || defined(GK6106)
                    if( device == GD_SYS_BOOT_DEVICE_SFLASH )
                    {
                        address = u32words * sizeof(U32);
                    }
                    else
                    {
                        address = ( u32words * sizeof(U32) ) / sizeof(U16);
                    }
                #else // SMARTMPEG, SMARTMPEG-L, SMARTMPEG-E
                    address = ( u32words * sizeof(U32) ) / sizeof(U16);
                #endif
                
                status = flash_get_sector( handle, address, &sector );
                *sectors = sector + 1;  
            }
            else
            {
                u32words = 0;
                *sectors = 0;
            }
        }  
    }
    flash_close( &handle );
    if( status != GD_OK )
    {
        u32words = 0;
        *sectors = 0;
    }
    //GM_Printf( "first free (s)flash address = %d\n", address );
    //GM_Printf( "used boot image sectors = %d\n", *sectors );
 
    return( status );
}