source: umon/main/common/cf.c @ 87db514

Last change on this file since 87db514 was 87db514, checked in by Amar Takhar <amar@…>, on 04/16/15 at 19:26:21

Initial commit of the umon repository.

Prior to this three changes were made:

  • Remove umon_ prefix from parent directories.
  • Collapse main/target/ into main/
  • Remove ports/template/flashtest.scr.ucon script.
  • Property mode set to 100644
File size: 4.6 KB
Line 
1/**************************************************************************
2 *
3 * Copyright (c) 2013 Alcatel-Lucent
4 *
5 * Alcatel Lucent licenses this file to You under the Apache License,
6 * Version 2.0 (the "License"); you may not use this file except in
7 * compliance with the License.  A copy of the License is contained the
8 * file LICENSE at the top level of this repository.
9 * You may also obtain a copy of the License at:
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 **************************************************************************
20 *
21 * cf.c:
22 * This code is the user interface portion of the cf (compact flash)
23 * command for uMon.
24 * This command is intended to be the "interface" portion of some
25 * other command (for example "fatfs").  Refer to the discussion in
26 * fatfs.c for more details.
27 *
28 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
29 *
30 */
31 
32#include "config.h"
33#if INCLUDE_CF
34#include "stddefs.h"
35#include "genlib.h"
36#include "cli.h"
37#include "tfs.h"
38#include "tfsprivate.h"
39#include "cf.h"
40
41
42char *CfHelp[] = {
43        "Compact Flash Interface",
44        "[options] {operation} [args]...",
45#if INCLUDE_VERBOSEHELP
46        "",
47        "Options:",
48        " -v     enable verbosity",
49        "",
50        "Operations:",
51        " init [prefix]",
52        " read {dest} {blk} {blktot}",
53        " write {blk} {src} {blktot}",
54#endif
55        0
56};
57
58static int cfInum;              /* Interface number: to support multiple CF interfaces.
59                                                 * Typically this will always be zero.
60                                                 */
61
62int
63CfCmd(int argc, char *argv[])
64{
65        char *cmd, *buf, *prefix, varname[16];
66        int opt, verbose, cfret, blknum, blkcnt;
67
68        verbose = 0;
69        while ((opt=getopt(argc,argv,"i:r:w:v")) != -1) {
70                switch(opt) {
71                        case 'i':
72                                cfInum = atoi(optarg);  /* sticky */
73                                break;
74                        case 'v':
75                                verbose = 1;
76                                break;
77                        default:
78                                return(CMD_PARAM_ERROR);
79                }
80        }
81
82        if (argc < optind + 1)
83                return(CMD_PARAM_ERROR);
84
85        cmd = argv[optind];
86
87        if (strcmp(cmd,"init") == 0) {
88                if (argc != optind+2)
89                        return(CMD_PARAM_ERROR);
90
91                prefix = argv[optind+1];
92                if (strlen(prefix)+4 > sizeof(varname)) {
93                        printf("prefix %s too long\n",prefix);
94                        return(CMD_PARAM_ERROR);
95                }
96
97                cfret = cfInit(cfInum,verbose);
98                if (cfret < 0) {
99                        printf("cfInit returned %d\n",cfret);
100                        return(CMD_FAILURE);
101                }
102
103                sprintf(varname,"%s_RD",prefix);
104                shell_sprintf(varname,"0x%lx",(long)cfRead);
105
106                sprintf(varname,"%s_WR",prefix);
107                shell_sprintf(varname,"0x%lx",(long)cfWrite);
108
109                shell_sprintf("CF_BLKSIZ","0x%lx",CF_BLKSIZE);
110
111                if (verbose) {
112                        printf("read: 0x%lx, write: 0x%lx, blksiz: 0x%lx\n",
113                                (long)cfRead,(long)cfWrite,(long)CF_BLKSIZE,verbose);
114                }
115        }
116        else if (strcmp(cmd,"read") == 0) {
117                if (argc != optind+4)
118                        return(CMD_PARAM_ERROR);
119
120                buf = (char *)strtoul(argv[optind+1],0,0);
121                blknum = strtoul(argv[optind+2],0,0);
122                blkcnt = strtoul(argv[optind+3],0,0);
123
124                cfret = cfRead(cfInum,buf,blknum,blkcnt,verbose);
125                if (cfret < 0) {
126                        printf("cfRead returned %d\n",cfret);
127                        return(CMD_FAILURE);
128                }
129        }
130        else if (strcmp(cmd,"write") == 0) {
131                if (argc != optind+4)
132                        return(CMD_PARAM_ERROR);
133                buf = (char *)strtoul(argv[optind+1],0,0);
134                blknum = strtoul(argv[optind+2],0,0);
135                blkcnt = strtoul(argv[optind+3],0,0);
136
137                cfret = cfWrite(cfInum,buf,blknum,blkcnt,verbose);
138                if (cfret < 0) {
139                        printf("cfWrite returned %d\n",cfret);
140                        return(CMD_FAILURE);
141                }
142        }
143        else {
144                printf("cf op <%s> not found\n",cmd);
145                return(CMD_FAILURE);
146        }
147
148        return(CMD_SUCCESS);
149}
150
151#ifdef INCLUDE_CF_DUMMY_FUNCS
152/* This code is included here just for simulating the CF
153 * interface (temporarily if a real one isn't ready.  In a real system,
154 * the INCLUDE_CF_DUMMY_FUNCS definition would be off.
155 */
156
157int
158cfInit(int interface, int verbose)
159{
160        if (interface != 0) {
161                if (verbose)
162                        printf("cfInit bad interface %d\n",interface);
163                return(-1);
164        }
165
166        return(0);
167}
168
169int
170cfRead(int interface, char *buf, int blk, int blkcnt, int verbose)
171{
172        char *from;
173        int     size;
174
175        if (interface != 0) {
176                if (verbose)
177                        printf("cfRead bad interface %d\n",interface);
178                return(-1);
179        }
180
181        from = (char *)(blk * CF_BLKSIZE);
182        size = blkcnt * CF_BLKSIZE;
183        memcpy(buf,from,size);
184        return(0);
185}
186
187int
188cfWrite(int interface, char *buf, int blk, int blkcnt, int verbose)
189{
190        char *to;
191        int     size;
192
193        if (interface != 0) {
194                if (verbose)
195                        printf("cfWrite bad interface %d\n",interface);
196                return(-1);
197        }
198
199        to = (char *)(blk * CF_BLKSIZE);
200        size = blkcnt * CF_BLKSIZE;
201        memcpy(to,buf,size);
202        return(0);
203}
204
205#endif
206
207#endif
Note: See TracBrowser for help on using the repository browser.