summaryrefslogtreecommitdiffstats
path: root/uClinux-2.4.20-uc1/drivers/block/wbflash/image.c
blob: 800fc2fc1fc47ddb3a8b2f7f35d6b3c5d5bcaf75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <asm/arch/flash.h>
#include <asm/semaphore.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/config.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <asm/fcntl.h>
#include <asm/unistd.h>
#include <asm/uaccess.h>
#include "cfi.h"

extern struct semaphore spare_lock;



int FindFooter(tfooter *** image_footer)
{
	static tfooter * image_footers[MAX_FOOTER_NUM];
	unsigned int footer_num=0;
	//int flash_type;
	unsigned int blocksize,addr,i;
	tfooter * footer;
	unsigned int * p;
	unsigned long long sum;
	
	*image_footer=image_footers;

	if(cfiGetFlashSize() == 0 )
		return -1;

	addr=FLASH_BASE+BOOTER_BLOCK_LENGTH;	// the 64kb of flash was preservied to boot loader
	blocksize=cfiGetBlockSize(addr);
	while( blocksize!= 0 )
	{
		addr+=blocksize;
		footer=(tfooter *)(addr-sizeof(tfooter));
		if(footer->signature==SIGNATURE_WORD)
		{
			p=(unsigned int *)footer;
			sum=0;
			for(i=0;i<sizeof(tfooter)/4-1;i++)// Not include the checksum
			{
				sum+=*(p+i);
			}
			sum = ~((sum&(-1LU))+(sum>>32));
			if( (unsigned int)sum == footer->checksum )					
				image_footers[footer_num++]=footer;
		}
		
		blocksize=cfiGetBlockSize(addr);
	}
	
	return footer_num;
}

/*1 success 0 failure*/
asmlinkage int sys_FindImage(unsigned int image_num, tfooter ** image_footer)
{
	int footer_num;
	tfooter ** footer;
	int i;

	
	footer_num=FindFooter(&footer);
	if(footer_num<=0)
	{
		return 0;
	}

	for(i=0;i<footer_num;i++)
	{
		if( footer[i]->num == image_num )
		{
			*image_footer=footer[i];
			return 1;
		}
	}
	

	return 0;
}



/*0 success*/
asmlinkage int sys_DelImage(unsigned int image_num)
{
	unsigned int flash_type;
	tfooter * footer;
	unsigned int addr;
	unsigned int blocksize;
	unsigned int end;
	unsigned long flags;
	
	if( sys_FindImage( image_num, &footer) )
	{
		if(cfiGetFlashSize() == 0) {
			printk("delete failed\n");
			return -EINVAL;
		}	
		addr=footer->base;
		blocksize = cfiGetBlockSize(addr);
		end=footer->base+footer->length;

		end+=sizeof(tfooter);
		
		//printk("end at:%x", end);

		if((end & (blocksize-1)) == 0 ) 
			end -= blocksize;
		else 
			end = end&(~(blocksize-1));
		
		//printk("end at:%x", end);

		
		{
			down(&spare_lock);
			save_flags(flags); cli();

			cfiGetBlockSize(end);
			cfiCmd.erase(end,blocksize);
			restore_flags(flags);
			up(&spare_lock);
		}

		return 0;
	}
	printk("delete image failed\n");
	return -EINVAL;
}

/*0 success 1 Corrupt*/
asmlinkage int sys_CorruptCheck(tfooter * image_footer)
{
	int footer_num;
	tfooter ** footer;
	int i;
	unsigned int a0,a1,b0,b1;

	if( image_footer->base < (FLASH_BASE+BOOTER_BLOCK_LENGTH) )return 1;
	if( image_footer->base+image_footer->length > (FLASH_BASE+cfiGetFlashSize()) )return 1;
	footer_num=FindFooter(&footer);

	if(footer_num<=0)
		return 0;
	for(i=0;i<footer_num;i++)
	{
		if( image_footer->num == footer[i]->num ) {
			printk("iamge exists");
			return 1;
		}	
	}

	
	b0=image_footer->base;
	if( FLASH_BLOCK_SIZE-(image_footer->length%FLASH_BLOCK_SIZE) < sizeof(tfooter) ) 
		b1=image_footer->base+image_footer->length+FLASH_BLOCK_SIZE;
	else
		b1=image_footer->base+image_footer->length;
	for(i=0;i<footer_num;i++)
	{
		a0=footer[i]->base;
		if( (FLASH_BLOCK_SIZE-(footer[i]->length%FLASH_BLOCK_SIZE) < sizeof(tfooter)) || !(footer[i]->length&(FLASH_BLOCK_SIZE-1)) ) 
			a1=footer[i]->base+footer[i]->length+FLASH_BLOCK_SIZE;
		else
			a1=footer[i]->base+footer[i]->length;
		if( (b0 >= a0) && (b0 < a1) )return 1;
		if( (b1 >= a0) && (b1 < a1) )return 1;
		if( (a0 > b0) && (a1 < b1) )return 1;
	}	

	return 0;
}