summaryrefslogtreecommitdiff
path: root/main.c
blob: 892cfc61df7f7c200cd1f3eefbf0e0dee9f33726 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * RTEMS Project (http://www.rtems.org/)
 *
 * Copyright 2012 Chris Johns (chrisj@rtems.org)
 */

/**
 * Run Time Linker test program.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>

#include <dlfcn.h>

#include <rtems.h>
#include <rtems/bdbuf.h>
#include <rtems/blkdev.h>
#include <rtems/diskdevs.h>
#include <rtems/error.h>
#include <rtems/fsmount.h>
#include <rtems/ramdisk.h>
#include <rtems/shell.h>
#include <rtems/untar.h>

#if RTEMS_APP_FLASHDISK
#include <rtems/flashdisk.h>
#include <libchip/am29lv160.h>
#endif

#if RTEMS_APP_IDEDISK
#include <libchip/ide_ctrl.h>
#include <libchip/ata.h>
#endif

#if pc586
#include "pc386-gdb.h"
int remote_debug;
#endif

#include <rtl.h>
#include <rap-shell.h>
#include <rtl-shell.h>
#include <rtl-trace.h>

#include <dlfcn-shell.h>

/*
 *  The tarfile is built automatically externally so we need to account
 *  for the leading symbol on the names.
 */
#if (RTL_GLUE(__USER_LABEL_PREFIX__, 1) == RTL_GLUE(_, 1))
  #define SYM(_x) _x
#else
  #define SYM(_x) _ ## _x
#endif

/**
 * The shell-init tarfile parameters.
 */
extern int SYM(binary_fs_root_tar_start);
extern int SYM(binary_fs_root_tar_size);

#define TARFILE_START SYM(binary_fs_root_tar_start)
#define TARFILE_SIZE  SYM(binary_fs_root_tar_size)

/**
 * The RAM Disk configuration.
 */
rtems_ramdisk_config rtems_ramdisk_configuration[] =
{
  {
    block_size: 512,
    block_num:  3 * 1024 * 2,
    location:   NULL
  }
};

/**
 * The number of RAM Disk configurations.
 */
size_t rtems_ramdisk_configuration_size = 1;

/**
 * Create the RAM Disk Driver entry.
 */
rtems_driver_address_table rtems_ramdisk_io_ops = {
  initialization_entry: ramdisk_initialize,
  open_entry:           rtems_blkdev_generic_open,
  close_entry:          rtems_blkdev_generic_close,
  read_entry:           rtems_blkdev_generic_read,
  write_entry:          rtems_blkdev_generic_write,
  control_entry:        rtems_blkdev_generic_ioctl
};

#if RTEMS_APP_FLASHDISK
/**
 * The Flash Device Driver configuration.
 */
const rtems_am29lv160_config rtems_am29lv160_configuration[] =
{
  {
    bus_8bit: 0,
    base:     (void*) 0xFFE00000
  }
};

/**
 * The number of AM29LV160 configurations.
 */
uint32_t rtems_am29lv160_configuration_size = 1;

/**
 * The Flash Segment descriptor. The mcf5235 board uses the
 * bottom part of the flash for the dBug monitor.
 */
const rtems_fdisk_segment_desc rtems_mcf5235_segment_descriptor[] =
{
  {
    count:   26,
    segment: 0,
    offset:  0x00050000,
    size:    RTEMS_FDISK_KBYTES (64)
  }
};

/**
 * The Flash Device descriptor.
 */
const rtems_fdisk_device_desc rtems_mcf5235_device_descriptor[] =
{
  {
    segment_count: 1,
    segments:      &rtems_mcf5235_segment_descriptor[0],
    flash_ops:     &rtems_am29lv160_handlers
  }
};

/**
 * The Flash Disk configuration.
 */
const rtems_flashdisk_config rtems_flashdisk_configuration[] =
{
  {
    block_size:         512,
    device_count:       1,
    devices:            &rtems_mcf5235_device_descriptor[0],
    flags:              RTEMS_FDISK_BLANK_CHECK_BEFORE_WRITE,
    unavail_blocks:     256,
    compact_segs:       100,
    avail_compact_segs: 100,
    info_level:         0
  }
};

/**
 * The number of Flash Disk configurations.
 */
size_t rtems_flashdisk_configuration_size = 1;

/**
 * Create the Flash Disk Driver entry.
 */
rtems_driver_address_table rtems_flashdisk_io_ops = {
  initialization_entry: rtems_fdisk_initialize,
  open_entry:           rtems_blkdev_generic_open,
  close_entry:          rtems_blkdev_generic_close,
  read_entry:           rtems_blkdev_generic_read,
  write_entry:          rtems_blkdev_generic_write,
  control_entry:        rtems_blkdev_generic_ioctl
};
#endif

#if RTEMS_APP_IDEDISK
/**
 * Create the IDE Disk Driver entry.
 */
rtems_driver_address_table rtems_idedisk_io_ops = {
  initialization_entry: rtems_ata_initialize,
  open_entry:           rtems_blkdev_generic_open,
  close_entry:          rtems_blkdev_generic_close,
  read_entry:           rtems_blkdev_generic_read,
  write_entry:          rtems_blkdev_generic_write,
  control_entry:        rtems_blkdev_generic_ioctl
};
#endif

/**
 * Let the IO system allocation the next available major number.
 */
#define RTEMS_DRIVER_AUTO_MAJOR (0)

/**
 * Start the RTEMS Shell.
 */
static void
shell_start (void)
{
  rtems_status_code sc;
  printf ("Starting shell....\n\n");
  sc = rtems_shell_init ("fstst", 60 * 1024, 150, "/dev/console", 0, 1, NULL);
  if (sc != RTEMS_SUCCESSFUL)
    printf ("error: starting shell: %s (%d)\n", rtems_status_text (sc), sc);
}

/**
 * Run the /shell-init script.
 */
static void
shell_init_script (void)
{
  rtems_status_code sc;
  printf ("Running /shell-init....\n\n");
  sc = rtems_shell_script ("fstst", 60 * 1024, 160, "/shell-init", "stdout",
                           0, 1, 1);
  if (sc != RTEMS_SUCCESSFUL)
    printf ("error: running shell script: %s (%d)\n", rtems_status_text (sc), sc);
}

static int
setup_ramdisk (void)
{
  rtems_device_major_number major;
  rtems_status_code         sc;

  /*
   * Register the RAM Disk driver.
   */
  printf ("Register RAM Disk Driver: ");
  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
                                 &rtems_ramdisk_io_ops,
                                 &major);
  if (sc != RTEMS_SUCCESSFUL)
  {
    printf ("error: ramdisk driver not initialised: %s\n",
            rtems_status_text (sc));
    return 1;
  }

  printf ("successful\n");

  return 0;
}

static int
setup_flashdisk (void)
{
#if RTEMS_APP_FLASHDISK
  rtems_device_major_number major;
  rtems_status_code         sc;

  /*
   * Register the Flash Disk driver.
   */
  printf ("Register Flash Disk Driver: ");
  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
                                 &rtems_flashdisk_io_ops,
                                 &major);
  if (sc != RTEMS_SUCCESSFUL)
  {
    printf ("error: flashdisk driver not initialised: %s\n",
            rtems_status_text (sc));
    return 1;
  }

  printf ("successful\n");
#endif
  return 0;
}

#if RTEMS_APP_IDEDISK
extern rtems_status_code rtems_ide_part_table_initialize (const char* );
#endif

static int
setup_idedisk (const char* path)
{
#if RTEMS_APP_IDEDISK
  rtems_status_code sc;

  /*
   * Register the IDE Disk driver.
   */
  printf ("Read IDE Disk Partition Table: ");
  sc = rtems_ide_part_table_initialize (path);
  if (sc != RTEMS_SUCCESSFUL)
    printf ("error: ide partition table not found: %s\n",
            rtems_status_text (sc));
  else
    printf ("successful\n");

#endif
  return 0;
}

static int
setup_rootfs (void)
{
  rtems_status_code sc;

  printf("Loading filesystem: ");

  sc = Untar_FromMemory((void *)(&TARFILE_START), (size_t)&TARFILE_SIZE);

  if (sc != RTEMS_SUCCESSFUL)
  {
    printf ("error: untar failed: %s\n", rtems_status_text (sc));
    return 1;
  }

  printf ("successful\n");

  return 0;
}

static int
shell_flash_erase (int argc, char* argv[])
{
#if RTEMS_APP_FLASHDISK
  const char* driver = NULL;
  int         arg;
  int         fd;

  for (arg = 1; arg < argc; arg++)
  {
    if (argv[arg][0] == '-')
    {
      printf ("error: invalid option: %s\n", argv[arg]);
      return 1;
    }
    else
    {
      if (!driver)
        driver = argv[arg];
      else
      {
        printf ("error: only one driver name allowed: %s\n", argv[arg]);
        return 1;
      }
    }
  }

  printf ("erase flash disk: %s\n", driver);

  fd = open (driver, O_WRONLY, 0);
  if (fd < 0)
  {
    printf ("error: flash driver open failed: %s\n", strerror (errno));
    return 1;
  }

  if (ioctl (fd, RTEMS_FDISK_IOCTL_ERASE_DISK) < 0)
  {
    printf ("error: flash driver erase failed: %s\n", strerror (errno));
    return 1;
  }

  close (fd);

  printf ("flash disk erased successful\n");
#endif
  return 0;
}

int
main (int argc, char* argv[])
{
  struct termios term;
  int            ret;

#if pc586
  int arg;
  for (arg = 1; arg < argc; arg++)
    if (strcmp (argv[arg], "--gdb") == 0)
      pc386_gdb_init ();
#endif

  if (tcgetattr(fileno(stdout), &term) < 0)
    printf ("error: cannot get terminal attributes: %s\n", strerror (errno));
  cfsetispeed (&term, B115200);
  cfsetospeed (&term, B115200);
  if (tcsetattr (fileno(stdout), TCSADRAIN, &term) < 0)
    printf ("error: cannot set terminal attributes: %s\n", strerror (errno));

  if (tcgetattr(fileno(stdin), &term) < 0)
    printf ("error: cannot get terminal attributes: %s\n", strerror (errno));
  cfsetispeed (&term, B115200);
  cfsetospeed (&term, B115200);
  if (tcsetattr (fileno(stdin), TCSADRAIN, &term) < 0)
    printf ("error: cannot set terminal attributes: %s\n", strerror (errno));

  printf ("\nRTEMS Run Time Link Editor Test, Version " \
          PACKAGE_VERSION "\n\n");

  ret = setup_ramdisk ();
  if (ret)
    exit (ret);

  ret = setup_flashdisk ();
  if (ret)
    exit (ret);

  ret = setup_idedisk ("/dev/hda");
  if (ret)
    exit (ret);

  ret = setup_idedisk ("/dev/hdb");
  if (ret)
    exit (ret);

  ret = setup_rootfs ();
  if (ret)
    exit (ret);

#if RTEMS_RTL_TRACE
  rtems_shell_add_cmd ("rtl-trace", "misc",
                       "RLT trace",
                       rtems_rtl_trace_shell_command);
#endif

  rtems_shell_add_cmd ("fderase", "misc",
                       "fderase driver", shell_flash_erase);
  rtems_shell_add_cmd ("rtl", "misc",
                       "Runtime Linker", rtems_rtl_shell_command);
  rtems_shell_add_cmd ("rap", "misc",
                       "Application loader", shell_rap);
  rtems_shell_add_cmd ("dlo", "misc",
                       "load object file", shell_dlopen);
  rtems_shell_add_cmd ("dlc", "misc",
                       "unload object file", shell_dlclose);
  rtems_shell_add_cmd ("dls", "misc",
                       "symbol search file", shell_dlsym);
  rtems_shell_add_cmd ("dlx", "misc",
                       "execute a call to the symbol", shell_dlcall);

  shell_init_script ();

  while (true)
    shell_start ();

  rtems_task_delete (RTEMS_SELF);

  return 0;
}

#if pc586
void
rtems_fatal_error_occurred (uint32_t code)
{
  printf ("fatal error: %08lx\n", code);
  for (;;);
}
#endif