OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [cmds/] [hdbug.c] - Blame information for rev 245

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marcus.erl
/*
2
    hdbug.c -- harddisk debugging
3
    Copyright (C) 2002 Richard Herveille, rherveille@opencores.org
4
 
5
    This file is part of OpenRISC 1000 Reference Platform Monitor (ORPmon)
6
 
7
    This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 2 of the License, or
10
    (at your option) any later version
11
 
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
 
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
*/
21
 
22
 
23
#include "support.h"
24
#include "common.h"
25
#include "dos.h"
26
#include "hdbug.h"
27
#include <ctype.h>
28
 
29
 
30
static int hdbug_num_commands;
31
static command_struct hdbug_command[MAX_HDBUG_COMMANDS];
32
 
33
static struct dosparam _dos_params;
34
static struct dosparam *dos_params = &_dos_params;
35
 
36
 
37
/**********************************************************************/
38
/*                                                                    */
39
/*      H D B U G                                                     */
40
/*                                                                    */
41
/**********************************************************************/
42
/*
43
        H D B U G _ I N I T
44
 
45
        initializes the ata core, mounts the DOS file system, and
46
        provides methods for accessing DOS drives
47
*/
48
void module_hdbug_init (void)
49
{
50
  hdbug_num_commands = 0;
51
 
52
  register_command ("hdbug", "<device> [<mode>]", "Opens ata device <device> & mounts DOS filesystem", hdbug_mount_cmd);
53
  register_hdbug_command ("umount", "", "Unmounts DOS filesystem & Closes device", hdbug_umount_cmd);
54
 
55
  register_hdbug_command ("dir", "", "dos 'dir' command.", hdbug_dir_cmd);
56
  register_hdbug_command ("cd", "", "dos 'cd' command.", hdbug_cd_cmd);
57
  register_hdbug_command ("help", "", "Display this help message", hdbug_help);
58
  register_hdbug_command ("exit", "", "Exit hdbug and return to ORPmon", hdbug_umount_cmd);
59
}
60
 
61
 
62
/*
63
 The next code is graceously taken from the "common.c" file
64
 and slightly modified.
65
 
66
 Better would be if we could access the routines in 'common.c'
67
 directly, using our own set of commands.
68
*/
69
 
70
/* Process command-line, generate arguments */
71
int hdbug_mon_command(void)
72
{
73
  char c = '\0';
74
  char str[1000];
75
  char *pstr = str;
76
  char *command_str;
77
  char *argv[20];
78
  int argc = 0;
79
 
80
 
81
  /* Show prompt */
82
  printf ("\nhdbug> ");
83
 
84
 
85
  /* Get characters from UART */
86
  c = getc();
87
  while (c != '\r' && c != '\f' && c != '\n')
88
  {
89
    if (c == '\b')
90
      pstr--;
91
    else
92
      *pstr++ = c;
93
    putc(c);
94
    c = getc();
95
  }
96
  *pstr = '\0';
97
  printf ("\n");
98
 
99
  /* Skip leading blanks */
100
  pstr = str;
101
  while ( isspace(*pstr) ) pstr++;
102
 
103
  /* Get command from the string */
104
  command_str = pstr;
105
 
106
  while (1) {
107
    /* Go to next argument */
108
    while ( isgraph(*pstr) ) pstr++;
109
    if (*pstr) {
110
      *pstr++ = '\0';
111
      while ( isspace(*pstr) ) pstr++;
112
      argv[argc++] = pstr;
113
    }
114
    else
115
      break;
116
  }
117
 
118
  return execute_hdbug_command(command_str, argc, argv);
119
}
120
 
121
 
122
int execute_hdbug_command(char *command_str, int argc, char **argv)
123
{
124
  int i, found = 0;
125
 
126
  for (i = 0; i < hdbug_num_commands; i++)
127
    if ( !strcmp(command_str, hdbug_command[i].name) )
128
    {
129
      switch ( hdbug_command[i].func(argc, argv) )
130
      {
131
        case -1:
132
          printf ("Missing/wrong parameters, usage: %s %s\n", hdbug_command[i].name, hdbug_command[i].params);
133
          break;
134
 
135
        case -2:
136
          return -1;
137
      }
138
 
139
      found++;
140
      break;
141
    }
142
 
143
    if (!found)
144
      printf ("Unknown command. Type 'hdbug help' for help.\n");
145
 
146
  return 0;
147
}
148
 
149
 
150
void register_hdbug_command (const char *name, const char *params, const char *help, int (*func)(int argc, char *argv[]) )
151
{
152
  if (hdbug_num_commands < MAX_HDBUG_COMMANDS)
153
  {
154
    hdbug_command[hdbug_num_commands].name = name;
155
    hdbug_command[hdbug_num_commands].params = params;
156
    hdbug_command[hdbug_num_commands].help = help;
157
    hdbug_command[hdbug_num_commands].func = func;
158
    hdbug_num_commands++;
159
  }
160
  else
161
    printf ("hdbug-command '%s' ignored; MAX_COMMANDS limit reached\n", name);
162
}
163
 
164
int hdbug_help(int argc, char **argv)
165
{
166
  int i;
167
 
168
  for (i = 0; i < hdbug_num_commands; i++)
169
    printf ("%-15s %-17s -%s\n", hdbug_command[i].name, hdbug_command[i].params, hdbug_command[i].help);
170
 
171
  return 0;
172
}
173
 
174
 
175
 
176
 
177
/**********************************************************************/
178
/*                                                                    */
179
/*     H D B U G   C O M M A N D S E T                                */
180
/*                                                                    */
181
/**********************************************************************/
182
 
183
/*
184
        H D B U G _ M O U N T
185
 
186
        opens the ata-device and mounts the dos filesystem
187
*/
188
int hdbug_mount_cmd(int argc, char **argv)
189
{
190
  int error;
191
 
192
  if (argc != 2)
193
    return -1;
194
 
195
 
196
  /* try to open the requested device (read-only)                     */
197
  dos_params->inode.i_rdev = (ATA_BASE_ADDR >> 16) | (**argv - '0');
198
  dos_params->filp.f_mode = FMODE_READ;
199
 
200
  /* open device                                                      */
201
  if ( (error = dos_open(dos_params)) )
202
  {
203
    switch (error) {
204
      case EINVAL:
205
        printf( "Error, device busy.\n" ); /* standard response to EINVAL */
206
        break;
207
 
208
      case EIOCTLIARG:
209
        printf( "Error, invalid IOCTL call.\n" );
210
        break;
211
 
212
      case EOPENIDEV:
213
        printf( "Error, invalid device.\n" );
214
        break;
215
 
216
      case EOPENIHOST:
217
        printf( "Error, ata host controller not found.\n" );
218
        break;
219
 
220
      case EOPENNODEV:
221
        printf( "Error, ata-device not found.\n" );
222
        break;
223
 
224
      default:
225
        printf( "Unkown error.\n" );
226
        break;
227
    }
228
  }
229
  else
230
  {
231
    printf( "directory startsector: 0x%08lX\n", dos_params->ssector );
232
    printf( "cluster startsector  : 0x%08lX\n", dos_params->csector );
233
    printf( "cluster startentry   : 0x%08lX\n", dos_params->sentry );
234
 
235
    /* device is opened, filesystem is mounted, start command prompt  */
236
    while ( !hdbug_mon_command() );
237
  }
238
 
239
  return 0;
240
}
241
 
242
 
243
/*
244
        H D B U G _ U M O U N T
245
 
246
        unmounts the dos filesystem and closes the ata-device
247
*/
248
int hdbug_umount_cmd(int argc, char **argv)
249
{
250
    dos_release(dos_params);
251
 
252
    return -2;
253
}
254
 
255
 
256
/*
257
        H D B U G _ C D
258
*/
259
int hdbug_cd_cmd(int argc, char **argv)
260
{
261
  struct dos_dir_entry *entry;
262
 
263
  switch (argc) {
264
    case 0:
265
      /* display present working directory                            */
266
      printf( "FIXME: present working directory\n" );
267
      return 0;
268
 
269
    case 1:
270
      break;
271
 
272
    default:
273
      printf( "Too many arguments.\n" );
274
      return 0;
275
  }
276
 
277
  /* search for the requested directory                               */
278
  if ( !(entry = dos_dir_find_entry(dos_params, *argv)) )
279
  {
280
    printf( "The system cannot find the specified path.\n" );
281
    return 0;
282
  }
283
 
284
 
285
  return 0;
286
}
287
 
288
 
289
/*
290
        H D B U G _ D I R
291
*/
292
int hdbug_dir_cmd(int argc, char **argv)
293
{
294
    register int i;
295
 
296
    /* read the directory structures from the current directory
297
       and display the results                                        */
298
 
299
    /* TODO: Add sub-directories */
300
 
301
    /* get first cluster of current directory                         */
302
    dos_dir_cluster_reset(dos_params);
303
 
304
    for (i=0; i < dos_params->root_entries; i++)
305
        hdbug_dir_print( dos_dir_get_entry(dos_params, i) );
306
 
307
   return 0;
308
}
309
 
310
 
311
int hdbug_dir_print(struct dos_dir_entry *entry)
312
{
313
  unsigned long  ltmp;
314
  unsigned short stmp;
315
 
316
  char txt[9];
317
 
318
  switch (entry->name[0]) {
319
    case 0x00:
320
      /* empty entry */
321
      break;
322
 
323
    case 0xe5:
324
      /* deleted/removed entry */
325
      break;
326
 
327
    default:
328
      /* check if entry is a label                                    */
329
      if (entry->attribute & ATT_LAB)
330
      {
331
        printf( "LABEL: " );
332
        memcpy(txt, entry->name, 8);
333
        txt[8] = '\0';
334
        printf( "%s", txt);
335
        memcpy(txt, entry->ext, 3);
336
        txt[3] = '\0';
337
        printf( "%s\n", txt);
338
      }
339
      else
340
      {
341
        /* display date & time                                          */
342
        stmp = entry->date;
343
        swap(&stmp, sizeof(short) );
344
        printf( "%02d-%02d-%4d  ",stmp & 0x1f, (stmp >> 5) & 0xf, ((stmp >> 9) & 0xffff) +1980);
345
 
346
        stmp = entry->time;
347
        swap(&stmp, sizeof(short) );
348
        printf( "%02d:%02d  ", (stmp >> 11) & 0x1f, (stmp >> 5) & 0x3f );
349
 
350
        /* display directory bit                                        */
351
        printf( "%s  ", entry->attribute & ATT_DIR ? "<DIR>" : "     " );
352
 
353
        /* display filesize                                             */
354
        ltmp = entry->size;
355
        swap(&ltmp, sizeof(unsigned long) );
356
        printf( "%12ld  ", ltmp );
357
 
358
        /* replace the first 'space' in the name by an null char        */
359
        *(char*)memchr(entry->name, 0x20, 8) = '\0';
360
        printf( "%s", entry->name);
361
 
362
        /* add extension                                                */
363
        if (entry->ext[0] != 0x20)
364
        {
365
          printf( ".%3s", entry->ext);
366
        }
367
 
368
        printf("\n");
369
        break;
370
     }
371
  }
372
 
373
  return 0;
374
}
375
 
376
 
377
/*
378
        H D B U G   T O O L S
379
*/
380
inline void *swap(void *var, size_t size)
381
{
382
  switch(size) {
383
    case 1:
384
      return var;
385
 
386
    case 2:
387
    {
388
      unsigned short p = *(unsigned short*)var;
389
      *(unsigned short*)var = (p << 8) | (p >> 8);
390
      return var;
391
    }
392
 
393
    case 4:
394
    {
395
      unsigned long *p = (unsigned long*)var;
396
      *p = (*p << 24) | ( (*p & 0x0000ff00) << 8) | ( (*p & 0x00ff0000) >> 8) | (*p >> 24);
397
      return var;
398
    }
399
 
400
    default:
401
      return NULL;
402
  }
403
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.