Hi Marco,

I looked updater.c and and I take that code as example.

Well,
I removed all function as same as do_... (do_fat_fsload, do_protect, do_flerase, do_mem_cp). In this way, I could remove also strcpy() and sprintf() needed to made their parameters and the result is that I cleaned and emproved all code.

Now all works correctly.
I think that the rewrite of the code, indirectly solve some bugs of memory corruption.

In attach, the code...
and if anyone try it, I'm interesting to feedback or opinions.

Thanks at all.
Matteo.




Marco wrote:
Hi Matteo,

in my opinion you should follow the "philosophy" of updater.c under the
tools folder to erase the flash. This command is very interesting, let
me know if you fix your problem.

Regards,

Marco

Matteo Facchinetti ha scritto:
Hi,

I have add the possibility to automatic update entire flash using a USB
pendrive.

To make it, need the following steps:
1) insert usb pendrive. In pendrive must be present these files:
install.dat and the files we want to copy.
2) from uboot console: => usb_system_autoupdate
3) the system update start, reading information from install.dat file.

The install.dat file raws has this syntax:
<file_type> "<file_name>" <star_flash_addr_partition>
<end_flash_addr_partition>

file_type values: kernel, uboot, fdt, rootfs.

In attachment there's an example of install.dat and the source code.

Now using it on my system (mpc5200, uboot_1.3.1) but there's a problem
that I can't fix:
if try to update uboot partition, it freeze in line 211 when call
do_flerase().

debugging I can see that it freeze in:
cmd_flash.c
434:     printf ("Erased %d sectors\n", erased);
after uboot partition erasing.


Can you help me?


Thanks in advance.
Matteo Facchinetti








------------------------------------------------------------------------

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


/*
 * (C) Copyright 2008
 * Matteo Facchinetti, Sirius Electronic Systems S.R.L., [EMAIL PROTECTED]
 *
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <command.h>
#include <usb.h>
#include <fat.h>

#define INSTALL_DAT_FILENAME "install.dat"
#define INSTALL_DAT_PTR (unsigned char *)0x100000
#define MAX_FILE_TO_UPDATE 6


struct install_dat {
	int type;
	char file_name[255];
	unsigned long file_size;
	unsigned long flash_s_address;
	unsigned long flash_e_address;
	unsigned long mem_address;
};


struct install_dat files_to_update[MAX_FILE_TO_UPDATE];


static int parse_installdat(char* start_file_ptr, int file_size, struct install_dat fupdate[]) {

	char *open_quote=NULL, *file_ptr=start_file_ptr;
	char *end_file_ptr, *end_fladdr_ptr;
	int idx=0;
	

	fupdate[0].type = -1;
	*fupdate[0].file_name='\0';
	fupdate[0].flash_s_address = 0x00000000;
	fupdate[0].flash_e_address = 0x00000000;
	end_file_ptr = file_ptr + file_size;
	while (file_ptr < end_file_ptr)
	{
		switch (*file_ptr) {
			case '\n':
				open_quote=NULL;
				start_file_ptr = file_ptr; 
				files_to_update[idx].type = -1; 
				*fupdate[idx].file_name='\0';
				fupdate[idx].flash_s_address = 0x00000000;
				fupdate[idx].flash_e_address = 0x00000000;
				break;  
			
			case '"':
				if (open_quote) {
					/* close_quote --> copy quote content to filename */
					*file_ptr='\0';
					strcpy(fupdate[idx].file_name, open_quote); 
				} else {
					open_quote=file_ptr + 1;
					/* validate image type "uboot - kernel - rootfs" */
					*file_ptr='\0';
					if (strstr(start_file_ptr, "uboot"))
						fupdate[idx].type = 1; 
					else if (strstr(start_file_ptr, "kernel"))
						fupdate[idx].type = 2; 
					else if (strstr(start_file_ptr, "fdt"))
						fupdate[idx].type = 3; 
					else if (strstr(start_file_ptr, "rootfs"))
						fupdate[idx].type = 4; 
					else
						*open_quote='\n'; /* error: invalidate line */
				}			
				break;
				
			case '#':
				if (!fupdate[idx].flash_s_address) 
				{
					/* parse start address */
					file_ptr+=1;
					end_fladdr_ptr=file_ptr+8; 
					*end_fladdr_ptr='\0';
					fupdate[idx].flash_s_address = simple_strtoul(file_ptr, &end_fladdr_ptr, 16);
				}
				else
				{
					/* parse start address */
					file_ptr+=1;
					end_fladdr_ptr=file_ptr+8; 
					*end_fladdr_ptr='\n';
					fupdate[idx].flash_e_address = simple_strtoul(file_ptr, &end_fladdr_ptr, 16);
					
					if ((fupdate[idx].type != -1) && 
						(*fupdate[idx].file_name != '\0')) 
					{
						/* line parse OK */
						file_ptr+=8;
						idx++;
						open_quote=NULL;
						start_file_ptr = file_ptr;
					}
					else
						*file_ptr='\n'; /* error: invalidate line */
				}
				break;
		}

		file_ptr++;
	}
	
	return idx;
}


int do_usb_system_autoupdate(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) {

	int rc, usb_stor_curr_dev, num_files, i, fsize;
	unsigned long mem_ptr;
	struct install_dat* current;
	block_dev_desc_t *dev_desc=NULL;

	/* usb start */
	if (usb_init() < 0) 
	 	return 1; /* usb init fails */

	udelay(1000);
	
	usb_stor_curr_dev = usb_stor_scan(1);
	if (usb_stor_curr_dev < 0) {
		udelay(1000);
	    usb_stop();
		return 0; /* no pendrive found */
	}

	dev_desc=get_dev("usb",0);
	if (dev_desc==NULL) {
		puts ("\n** Invalid boot device **\n");
		usb_stop();
		goto error_exit;
	}
	if (fat_register_device(dev_desc, 1)!=0) {
		printf ("\n** Unable to use usb 0:1 for fatload **\n");
		usb_stop();
		goto error_exit;
	}

	puts("USB system autoupdate\n");
	
	/* 1 - read install.dat file. 
	 */
	fsize = file_fat_read (INSTALL_DAT_FILENAME, INSTALL_DAT_PTR, 0);
	if(fsize==-1) {
		printf("\n** Unable to read \"%s\" from usb 0:1 **\n",INSTALL_DAT_FILENAME);
		usb_stop();
		goto error_exit;
	}
	num_files = parse_installdat((char *) INSTALL_DAT_PTR, fsize, files_to_update);
	
	/* 2 - verify if ALL files to update are present and copy in memory. 
	 *
	 *		This ensures the complete system update described in install.dat
	 *		and prevents partial flash writing caused by the 
	 *		removal of USB disk or some error during copy.
	 */
	mem_ptr = 0x100000;
	for (i=0; i<num_files; i++)
	{	
		current = &files_to_update[i];
		fsize = file_fat_read (current->file_name, (unsigned char *)mem_ptr, 0);
		if(fsize==-1) {
			printf("\n** Unable to read \"%s\" from usb 0:1 **\n",INSTALL_DAT_FILENAME);
			usb_stop();
			goto error_exit;
		}
		current->file_size = fsize;
		current->mem_address = mem_ptr;

		mem_ptr += current->file_size;
	}
	
	usb_stop();

	/* 3 - copy files from memory to flash. 
	 */ 
	for (i=0; i<num_files; i++)
	{
		current = &files_to_update[i];
		flash_sect_protect(0, current->flash_s_address, current->flash_e_address);
		printf("Erasing ");
		flash_sect_erase(current->flash_s_address, current->flash_e_address);
		printf("Writing ");
		rc = flash_write((char *)current->mem_address, current->flash_s_address, current->file_size);
		if (rc != 0) printf("\nFlashing failed due to error %d\n", rc);
		else printf("\ndone\n");
		flash_sect_protect(1, current->flash_s_address, current->flash_e_address);

#ifdef DEBUG
		printf("N:%d \t\t type:%d\n", i, files_to_update[i].type);
		printf("file_name:%s [%ld], \n", files_to_update[i].file_name, files_to_update[i].file_size);
		printf("flash_s_address:%lx\n", files_to_update[i].flash_s_address);
		printf("flash_e_address:%lx\n", files_to_update[i].flash_e_address);
		printf("mem_ptr:%lx\n", files_to_update[i].mem_address);
		printf("-------------------------------------------------------------\n");
#endif
	}	

error_exit:
	udelay(1000*1000*5);
	return 1;
}



/* -------------------------------------------------------------------- */

extern int do_usb_system_autoupdate(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);

U_BOOT_CMD(
	usb_system_autoupdate, 1, 1, do_usb_system_autoupdate,
	"usb_system_autoupdate - Automatically update internal flash reading install.dat file on USB disk.\n",
	NULL
);

Attachment: install.dat
Description: MOPAC data

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to