コード例 #1
0
ファイル: flash.c プロジェクト: cilynx/dd-wrt
static void
fis_create(int argc, char *argv[])
{
    int i, stat;
    unsigned long length, img_size;
    CYG_ADDRESS mem_addr, exec_addr, flash_addr, entry_addr;
    char *name;
    bool mem_addr_set = false;
    bool exec_addr_set = false;
    bool entry_addr_set = false;
    bool flash_addr_set = false;
    bool length_set = false;
    bool img_size_set = false;
    bool no_copy = false;
    void *err_addr;
    struct fis_image_desc *img = NULL;
    bool defaults_assumed;
    struct option_info opts[7];
    bool prog_ok = true;

    init_opts(&opts[0], 'b', true, OPTION_ARG_TYPE_NUM, 
              (void *)&mem_addr, (bool *)&mem_addr_set, "memory base address");
    init_opts(&opts[1], 'r', true, OPTION_ARG_TYPE_NUM, 
              (void *)&exec_addr, (bool *)&exec_addr_set, "ram base address");
    init_opts(&opts[2], 'e', true, OPTION_ARG_TYPE_NUM, 
              (void *)&entry_addr, (bool *)&entry_addr_set, "entry point address");
    init_opts(&opts[3], 'f', true, OPTION_ARG_TYPE_NUM, 
              (void *)&flash_addr, (bool *)&flash_addr_set, "FLASH memory base address");
    init_opts(&opts[4], 'l', true, OPTION_ARG_TYPE_NUM, 
              (void *)&length, (bool *)&length_set, "image length [in FLASH]");
    init_opts(&opts[5], 's', true, OPTION_ARG_TYPE_NUM, 
              (void *)&img_size, (bool *)&img_size_set, "image size [actual data]");
    init_opts(&opts[6], 'n', false, OPTION_ARG_TYPE_FLG, 
              (void *)&no_copy, (bool *)0, "don't copy from RAM to FLASH, just update directory");
    if (!scan_opts(argc, argv, 2, opts, 7, (void *)&name, OPTION_ARG_TYPE_STR, "file name"))
    {
        fis_usage("invalid arguments");
        return;
    }

    fis_read_directory();
    defaults_assumed = false;
    if (name) {
        // Search existing files to acquire defaults for params not specified:
        img = fis_lookup(name, NULL);
        if (img) {
            // Found it, so get image size from there
            if (!length_set) {
                length_set = true;
                length = img->size;
                defaults_assumed = true;
            }
        }
    }
    if (!mem_addr_set && (load_address >= (CYG_ADDRESS)ram_start) &&
	(load_address_end) < (CYG_ADDRESS)ram_end) {
	mem_addr = load_address;
	mem_addr_set = true;
        defaults_assumed = true;
        // Get entry address from loader, unless overridden
        if (!entry_addr_set)
            entry_addr = entry_address;
	if (!length_set) {
	    length = load_address_end - load_address;
	    length_set = true;
	} else if (defaults_assumed && !img_size_set) {
	    /* We got length from the FIS table, so the size of the
	       actual loaded image becomes img_size */
	    img_size = load_address_end - load_address;
	    img_size_set = true;
	}
    }
    // Get the remaining fall-back values from the fis
    if (img) {
        if (!exec_addr_set) {
            // Preserve "normal" behaviour
            exec_addr_set = true;
            exec_addr = flash_addr_set ? flash_addr : mem_addr;
        }
        if (!flash_addr_set) {
            flash_addr_set = true;
            flash_addr = img->flash_base;
            defaults_assumed = true;
        }
    }

    if ((!no_copy && !mem_addr_set) || (no_copy && !flash_addr_set) ||
        !length_set || !name) {
        fis_usage("required parameter missing");
        return;
    }
    if (!img_size_set) {
        img_size = length;
    }
    // 'length' is size of FLASH image, 'img_size' is actual data size
    // Round up length to FLASH block size
#ifndef CYGPKG_HAL_MIPS // FIXME: compiler is b0rken
    length = ((length + flash_block_size - 1) / flash_block_size) * flash_block_size;
    if (length < img_size) {
        diag_printf("Invalid FLASH image size/length combination\n");
        return;
    }
#endif
    if (flash_addr_set &&
        ((stat = flash_verify_addr((void *)flash_addr)) ||
         (stat = flash_verify_addr((void *)(flash_addr+length-1))))) {
        _show_invalid_flash_address(flash_addr, stat);
        return;
    }
    if (flash_addr_set && ((flash_addr & (flash_block_size-1)) != 0)) {
        diag_printf("Invalid FLASH address: %p\n", (void *)flash_addr);
        diag_printf("   must be 0x%x aligned\n", flash_block_size);
        return;
    }
    if (strlen(name) >= sizeof(img->name)) {
        diag_printf("Name is too long, must be less than %d chars\n", (int)sizeof(img->name));
        return;
    }
    if (!no_copy) {
        if ((mem_addr < (CYG_ADDRESS)ram_start) ||
            ((mem_addr+img_size) >= (CYG_ADDRESS)ram_end)) {
            diag_printf("** WARNING: RAM address: %p may be invalid\n", (void *)mem_addr);
            diag_printf("   valid range is %p-%p\n", (void *)ram_start, (void *)ram_end);
        }
        if (!flash_addr_set && !fis_find_free(&flash_addr, length)) {
            diag_printf("Can't locate %lx(%ld) bytes free in FLASH\n", length, length);
            return;
        }
    }
    // First, see if the image by this name has agreable properties
    if (img) {
        if (flash_addr_set && (img->flash_base != flash_addr)) {
            diag_printf("Image found, but flash address (%p)\n"
                        "             is incorrect (present image location %p)\n",
                        flash_addr, img->flash_base);
            
            return;
        }
        if (img->size != length) {
            diag_printf("Image found, but length (0x%lx, necessitating image size 0x%lx)\n"
                        "             is incorrect (present image size 0x%lx)\n",
                        img_size, length, img->size);
            return;
        }
        if (!verify_action("An image named '%s' exists", name)) {
            return;
        } else {                
            if (defaults_assumed) {
                if (no_copy &&
                    !verify_action("* CAUTION * about to program '%s'\n            at %p..%p from %p", 
                                   name, (void *)flash_addr, (void *)(flash_addr+img_size-1),
                                   (void *)mem_addr)) {
                    return;  // The guy gave up
                }
            }
        }
    } else {
#ifdef CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS
        // Make sure that any FLASH address specified directly is truly free
        if (flash_addr_set && !no_copy) {
            struct free_chunk chunks[CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS];
            int idx, num_chunks;
            bool is_free = false;

            num_chunks = find_free(chunks);
            for (idx = 0;  idx < num_chunks;  idx++) {
                if ((flash_addr >= chunks[idx].start) && 
                    ((flash_addr+length-1) <= chunks[idx].end)) {
                    is_free = true;
                }
            }
            if (!is_free) {
                diag_printf("Invalid FLASH address - not free!\n");
                return;
            }
        }
#endif
        // If not image by that name, try and find an empty slot
        img = (struct fis_image_desc *)fis_work_block;
        for (i = 0;  i < fisdir_size/sizeof(*img);  i++, img++) {
            if (img->name[0] == (unsigned char)0xFF) {
                break;
            }
        }
    }
    if (!no_copy) {
        // Safety check - make sure the address range is not within the code we're running
        if (flash_code_overlaps((void *)flash_addr, (void *)(flash_addr+img_size-1))) {
            diag_printf("Can't program this region - contains code in use!\n");
            return;
        }
        if (prog_ok) {
            // Erase area to be programmed
            if ((stat = flash_erase((void *)flash_addr, length, (void **)&err_addr)) != 0) {
                diag_printf("Can't erase region at %p: %s\n", err_addr, flash_errmsg(stat));
                prog_ok = false;
            }
        }
        if (prog_ok) {
            // Now program it
            if ((stat = FLASH_PROGRAM((void *)flash_addr, (void *)mem_addr, img_size, (void **)&err_addr)) != 0) {
                diag_printf("Can't program region at %p: %s\n", err_addr, flash_errmsg(stat));
                prog_ok = false;
            }
        }
    }
    if (prog_ok) {
        // Update directory
        memset(img, 0, sizeof(*img));
        strcpy(img->name, name);
        img->flash_base = flash_addr;
        img->mem_base = exec_addr_set ? exec_addr : (flash_addr_set ? flash_addr : mem_addr);
        img->entry_point = entry_addr_set ? entry_addr : (CYG_ADDRESS)entry_address;  // Hope it's been set
        img->size = length;
        img->data_length = img_size;
#ifdef CYGSEM_REDBOOT_FIS_CRC_CHECK
        if (!no_copy) {
            img->file_cksum = cyg_crc32((unsigned char *)mem_addr, img_size);
        } else {
            // No way to compute this, sorry
            img->file_cksum = 0;
        }
#endif
        fis_update_directory();
    }
}
コード例 #2
0
HRESULT fis_create_progress(CYG_ADDRESS mem_addr, uint32 length, 
		   CYG_ADDRESS exec_addr, CYG_ADDRESS entry_addr,
		   char *name, BOOL bDeleteIfNeeded, FIS_PROGRESS_FUNC progressFunc)
{
	int stat, i;
	unsigned long img_size;
	CYG_ADDRESS flash_addr;
	void *err_addr;
	struct fis_image_desc *img = NULL;
	BOOL bFlashAddrGood = FALSE;
	BOOL bDelOldImage = FALSE;
	FIS_PROGRESS_STRUCT progress;

	memset (&progress,0,sizeof(progress));
	memcpy(fis_work_block, fis_addr, fisdir_size);

	img_size = length;

	// 'length' is size of FLASH image, 'img_size' is actual data size
	// Round up length to FLASH block size
	length = ((length + flash_block_size - 1) / flash_block_size) * flash_block_size;
	if (length < img_size) 
	{
		SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Internal error in image\n\r");
		return E_FIS_ILLEGAL_IMAGE;
	}

	if (!name)
	{
		SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Internal error in image\n\r");
		return E_FIS_ILLEGAL_IMAGE;
	}
	if (strlen(name) >= sizeof(img->name))
	{
		SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Internal error in image\n\r");
		return E_FIS_ILLEGAL_IMAGE;
	}

	// Search existing files to acquire defaults for params not specified:
	img = fis_lookup(name, NULL);

	// If we have an image we need to check if it is any good
	if (img) 
	{
		if (length != img->size)
		{
			//in this case we either return an error or delete the old image
			if (!bDeleteIfNeeded) 
			{	
				SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Image already exist with different size\n\r");
				return E_FIS_ILLEGAL_IMAGE;
			}
			//We need to remember to delete the image
			bDelOldImage = TRUE;
			SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Delete old image\n\r");
		}
		else
		{
			bFlashAddrGood = TRUE;
			SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Reuse old image\n\r");
		}
	}
	else
	{
		//need to find a new free image
		img = (struct fis_image_desc *)fis_work_block;
		for (i = 0;  i < fisdir_size/sizeof(*img);  i++, img++)
		{
			if (img->name[0] == (unsigned char)0xFF) break;
		}
		if (img->name[0] != (unsigned char)0xFF)
		{
			SYS_DEBUG(SYSDEBUG_TRACE_FIS, "No more directory entries\n\r");
			return E_FIS_NO_SPACE;
		}
		SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Using new image\n\r");
	}
	//we definitely have an img pointer which is valid
	//Let's figure out the work we need to do for the progress info
	if (progressFunc) 
	{
		progress.func = progressFunc;
		progress.procur = 0;
		progress.promax = fis_flash_program_progressinfo(img_size);
		if (bDelOldImage || bFlashAddrGood) progress.promax += fis_flash_erase_progressinfo (img->size);
		if (!bFlashAddrGood) progress.promax += fis_flash_erase_progressinfo (length);
		progress.promax += 10; //to update the directory 		
		progressFunc(progress.promax, progress.procur);
	}

	if (bDelOldImage || bFlashAddrGood)
	{
	  if ((stat = fis_flash_erase_progress((void *)img->flash_base, img->size, &err_addr, &progress )) != 0)
		{	
			SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Flash Erase failed\n\r");
			return E_FIS_FLASH_OP_FAILED;
		}
	}
	if (!bFlashAddrGood)
	{
		//we need to find space
		if (!fis_find_free(&flash_addr, length))
		{	
			SYS_DEBUG(SYSDEBUG_TRACE_FIS, "No free space in flash\n\r");
			return E_FIS_NO_SPACE;
		}
	} 
	else
	{
		flash_addr = img->flash_base;
	}
	//at this point we have erased old stuff if needed and we are ready to program

	if (((stat = flash_verify_addr((void *)flash_addr)) ||
			 (stat = flash_verify_addr((void *)(flash_addr+img_size-1)))))
	{
		//this should not happen, it is an internal error
		SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Internal error, illegal flash address\n\r");
		return E_FIS_FLASH_OP_FAILED;
	}
	if (flash_addr & (flash_block_size-1))
	{
		//this should not happen, it is an internal error
		SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Internal error, illegal flash address\n\r");
		return E_FIS_FLASH_OP_FAILED;
	}

	// We need to erase the new are we found but it is likely clean so we will not progress it
	// as it will only be a blank check
	if (!bFlashAddrGood)
	{
	  if ((stat = fis_flash_erase_progress((void *)img->flash_base, img->size, &err_addr, &progress)) != 0)
		{	
			SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Flash erase failed\n\r");
			return E_FIS_FLASH_OP_FAILED;
		}		
	}

	if (mem_addr!=0xffffffff) //ML:Create image without programming if addr=0xffffffff
	{
		// Now program it
		if ((stat = fis_flash_program_progress((void *)flash_addr, (void *)mem_addr, img_size, &err_addr,&progress)) != 0)
		{
			SYS_DEBUG(SYSDEBUG_TRACE_FIS, "Flash program failed\n\r");
			return E_FIS_FLASH_OP_FAILED;
		}
	}
	// Update directory
	memset(img, 0, sizeof(*img));
	strcpy(img->name, name);
	img->flash_base = flash_addr;
	img->mem_base = exec_addr;
	img->entry_point = entry_addr;  // Hope it's been set
	img->size = length;
	img->data_length = img_size;
#ifdef CYGSEM_REDBOOT_FIS_CRC_CHECK
	img->file_cksum = cyg_crc32((unsigned char *)flash_addr, img_size);
#endif
	fis_update_directory();
	if (progressFunc) progressFunc(progress.promax, progress.promax);
	return NO_ERROR;
}