OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [utils/] [loader.c] - Blame information for rev 159

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

Line No. Rev Author Line
1 6 julius
/* Small utility that makes flash image. */
2
 
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <sys/types.h>
7
#include <sys/stat.h>
8
#include <unistd.h>
9
#include "../mad-xess/fsyst.h"
10
 
11
unsigned int swap (unsigned int x) {
12
  return (x & 0xFF) << 24
13
    | (x & 0xFF00) << 8
14
    | (x & 0xFF0000) >> 8
15
    | (x & 0xFF000000) >> 24;
16
}
17
 
18
/* Copies data from fi to fo. Returns nonzero
19
   if error. */
20
int copy_into (FILE *fo, FILE *fi)
21
{
22
  char buf[8192];
23
  int bread;
24
  do
25
    {
26
      bread = fread (&buf, 1, sizeof(buf), fi);
27
      if (bread != fwrite (&buf, 1, bread, fo))
28
        return 1;
29
    } while (bread == sizeof(buf));
30
  return 0;
31
}
32
 
33
/* Writes file to fo and returns error.  */
34
int write_file (FILE *fo, struct file_struct *file)
35
{
36
  unsigned int u;
37
  int ok = 0;
38
  u = swap(file->length);
39
  printf("%08x:%08x\n", file->length, u);
40
  if (fwrite(&u, sizeof(unsigned long), 1, fo))
41
    ok = 1;
42
  u = swap(file->type);
43
  if (fwrite(&u, sizeof(unsigned long), 1, fo) && ok)
44
    return 0;
45
  fprintf (stderr, "Cannot write to file.\n");
46
  return 1;
47
}
48
 
49
int main(int argc, char *argv[])
50
{
51
  int i;
52
  FILE *fo;
53
  struct file_struct file;
54
 
55
  if (argc <= 1)
56
    {
57
      printf ("Usage: loader image_file.mfs [file.mp3 [...]]\n");
58
      return 1;
59
    }
60
 
61
  if ((fo = fopen (argv[1], "wb+")) == NULL)
62
    {
63
      fprintf (stderr, "Cannot open output file '%s'\n", argv[1]);
64
      return 2;
65
    }
66
 
67
  file.type = FT_ROOT;
68
  file.length = HEADER_SIZE;
69
  if (write_file (fo, &file))
70
    return 3;
71
 
72
  for (i = 2; i < argc; i++)
73
    {
74
      FILE *fi = fopen (argv[i], "rb");
75
      struct stat fi_stat;
76
      int align;
77
      if (!fi)
78
        {
79
          fprintf (stderr, "Cannot open input file '%s'\n", argv[i]);
80
          return 1;
81
        }
82
      stat (argv[i], &fi_stat);
83
      printf ("Track %i: %s (size %i)\n", i - 1, argv[i], (int)fi_stat.st_size);
84
 
85
      file.type = FT_TRACK_NO;
86
      file.length = HEADER_SIZE + sizeof (unsigned int);
87
      file.data[0] = swap(i - 1);
88
      if (write_file (fo, &file))
89
        return 3;
90
      if (!fwrite (&file.data[0], sizeof (unsigned int), 1, fo))
91
        {
92
          fprintf (stderr, "Cannot write to file.\n");
93
          return 3;
94
        }
95
 
96
      file.type = FT_TRACK_NAME;
97
      align = (4 - ((strlen (argv[i]) + 1) & 3)) & 3;
98
      file.length = HEADER_SIZE + strlen (argv[i]) + 1 + align;
99
      if (write_file (fo, &file))
100
        return 3;
101
      if (!fwrite (argv[i], strlen (argv[i]) + 1 + align, 1, fo))
102
        {
103
          fprintf (stderr, "Cannot write to file.\n");
104
          return 3;
105
        }
106
 
107
      file.type = FT_TRACK_DATA;
108
      align = (4 - (fi_stat.st_size & 3)) & 3;
109
      file.length = HEADER_SIZE + fi_stat.st_size + align;
110
      if (write_file (fo, &file))
111
        return 3;
112
      copy_into(fo, fi);
113
      fwrite (&align, 1, align, fo);
114
      fclose (fi);
115
    }
116
  file.type = FT_END;
117
  file.length = 0;
118
  if (write_file (fo, &file))
119
    return 3;
120
  printf ("Done.\n");
121
  fclose (fo);
122
  return 0;
123
}

powered by: WebSVN 2.1.0

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