summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/ui/ui_test_flash.c
blob: 841d3fa74c0798f47c4a67c99b10dedae6bd15a4 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Flash Test commands			File: ui_test_flash.c
    *  
    *  Some commands to test the flash device interface.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_string.h"
#include "lib_queue.h"
#include "lib_malloc.h"
#include "lib_printf.h"

#include "cfe_iocb.h"
#include "cfe_device.h"
#include "cfe_console.h"
#include "cfe_devfuncs.h"
#include "cfe_timer.h"
#include "cfe_ioctl.h"

#include "cfe_error.h"

#include "ui_command.h"

int ui_init_flashtestcmds(void);

static int ui_cmd_flashtest(ui_cmdline_t *cmd,int argc,char *argv[]);
//static int ui_cmd_readnvram(ui_cmdline_t *cmd,int argc,char *argv[]);
//static int ui_cmd_erasenvram(ui_cmdline_t *cmd,int argc,char *argv[]);

int ui_init_flashtestcmds(void)
{
    cmd_addcmd("show flash",
	       ui_cmd_flashtest,
	       NULL,
	       "Display information about a flash device.",
	       "show flash [-sectors]",
	       "-sectors;Display sector information");

#if 0
    cmd_addcmd("nvram read",
	       ui_cmd_readnvram,
	       NULL,
	       "read the NVRAM",
	       "test nvram devname offset",
	       "");

    cmd_addcmd("nvram erase",
	       ui_cmd_erasenvram,
	       NULL,
	       "erase the NVRAM",
	       "erasenvram devname",
	       "-pattern");
#endif


    return 0;
}


static char *flashtypes[] = {
    "Unknown","SRAM","ROM","Flash"
};


static int ui_cmd_flashtest(ui_cmdline_t *cmd,int argc,char *argv[])
{
    flash_info_t info;
    int fd;
    int retlen;
    int res = 0;
    int idx;
    flash_sector_t sector;
    nvram_info_t nvraminfo;
    char *devname;
    int showsectors;

    devname = cmd_getarg(cmd,0);
    if (!devname) return ui_showusage(cmd);

    showsectors = cmd_sw_isset(cmd,"-sectors");

    fd = cfe_open(devname);
    if (fd < 0) {
	ui_showerror(fd,"Could not open flash device %s",devname);
	return fd;
	}

    res = cfe_ioctl(fd,IOCTL_FLASH_GETINFO,(uint8_t *) &info,sizeof(flash_info_t),&retlen,0);
    if (res == 0) {
	printf("FLASH: Base %016llX size %08X type %02X(%s) flags %08X\n",
	   info.flash_base,info.flash_size,info.flash_type,flashtypes[info.flash_type],
	       info.flash_flags);
	}
    else {
	printf("FLASH: Could not determine flash information\n");
	}

    res = cfe_ioctl(fd,IOCTL_NVRAM_GETINFO,(uint8_t *) &nvraminfo,sizeof(nvram_info_t),&retlen,0);
    if (res == 0) {
	printf("NVRAM: Offset %08X Size %08X EraseFlg %d\n",
	       nvraminfo.nvram_offset,nvraminfo.nvram_size,nvraminfo.nvram_eraseflg);
	}
    else {
	printf("NVRAM: Not supported by this flash\n");
	}

    if (showsectors && (info.flash_type == FLASH_TYPE_FLASH)) {
	printf("Flash sector information:\n");

	idx = 0;
	for (;;) {
	    sector.flash_sector_idx = idx;
	    res = cfe_ioctl(fd,IOCTL_FLASH_GETSECTORS,(uint8_t *) &sector,sizeof(flash_sector_t),&retlen,0);
	    if (res != 0) {
		printf("ioctl error\n");
		break;
		}
	    if (sector.flash_sector_status == FLASH_SECTOR_INVALID) break;
	    printf("  Sector %d offset %08X size %d\n",
		   sector.flash_sector_idx,
		   sector.flash_sector_offset,
		   sector.flash_sector_size);     
	    idx++;
	    }
	}

    cfe_close(fd);
    return 0;

}


#if 0
static int ui_cmd_readnvram(ui_cmdline_t *cmd,int argc,char *argv[])
{
    char *dev;
    char *tok;
    int fd;
    int offset = 0;
    int res;
    uint8_t buf[512];
    int idx;

    dev = cmd_getarg(cmd,0);
    if (!dev) return ui_showusage(cmd);

    tok = cmd_getarg(cmd,1);
    if (tok) offset = xtoi(tok);
    else offset = 0;

    fd = cfe_open(dev);
    if (fd < 0) {
	ui_showerror(fd,"could not open NVRAM");
	return fd;
	}

    res = cfe_readblk(fd,offset,buf,512);
    printf("Offset %d Result %d\n",offset,res);
    for (idx = 0; idx < 512; idx++) {
	if ((idx % 16) == 0) printf("\n");
	printf("%02X ",buf[idx]);
	}
    printf("\n");	

    cfe_close(fd);
    return 0;
	
}

static int ui_cmd_erasenvram(ui_cmdline_t *cmd,int argc,char *argv[])
{
    char *dev;
    int fd;
    uint8_t buffer[2048];
    int res;
    char *tok;
    int offset;
    int length;
    uint8_t data;

    dev = cmd_getarg(cmd,0);
    if (!dev) return ui_showusage(cmd);

    offset = 0;
    if ((tok = cmd_getarg(cmd,1))) offset = xtoi(tok);
    length = 512; 

    if ((tok = cmd_getarg(cmd,2))) length = xtoi(tok);
    if (length > 2048) length = 2048;

    data = 0xFF;
    if ((tok = cmd_getarg(cmd,3))) data = xtoi(tok);

    fd = cfe_open(dev);
    if (fd < 0) {
	ui_showerror(fd,"could not open NVRAM");
	return fd;
	}

    if (cmd_sw_isset(cmd,"-pattern")) {
	memset(buffer,0,sizeof(buffer));
	for (res = 0; res < 2048; res++) {
	    buffer[res] = res & 0xFF;
	    }
	}
    else memset(buffer,data,sizeof(buffer));

    printf("Fill offset %04X length %04X\n",offset,length);

    res = cfe_writeblk(fd,offset,buffer,length);

    printf("write returned %d\n",res);

    cfe_close(fd);
    return 0;
	
}
#endif