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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [ppc/] [boot/] [mk_type41.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1624 jcastillo
/*
2
 * This program will make a type 0x41 load image from an
3
 * executable file.  Note:  assumes that the executable has
4
 * already been "flattened" by 'mkboot'.
5
 *
6
 * usage: mk_type41 flat-file image
7
 */
8
 
9
#include <stdio.h>
10
#include <errno.h>
11
#include <sys/stat.h>
12
 
13
_LE(long val, unsigned char *le)
14
{
15
        le[0] = val;
16
        le[1] = val >> 8;
17
        le[2] = val >> 16;
18
        le[3] = val >> 24;
19
}
20
 
21
main(int argc, char *argv[])
22
{
23
        int in_fd, out_fd, len, size;
24
        struct stat info;
25
        char buf[8192];
26
        struct hdr
27
        {
28
                unsigned long entry_point;
29
                unsigned long image_length;
30
        } hdr;
31
        if (argc != 3)
32
        {
33
                fprintf(stderr, "usage: mk_type41 <boot-file> <image>\n");
34
                exit(1);
35
        }
36
        if ((in_fd = open(argv[1], 0)) < 0)
37
        {
38
                fprintf(stderr, "Can't open input file: '%s': %s\n", argv[1], strerror(errno));
39
                exit(2);
40
        }
41
        if ((out_fd = creat(argv[2], 0666)) < 0)
42
        {
43
                fprintf(stderr, "Can't create output file: '%s': %s\n", argv[2], strerror(errno));
44
                exit(2);
45
        }
46
        if (fstat(in_fd, &info) < 0)
47
        {
48
                fprintf(stderr, "Can't get info on input file: %s\n", strerror(errno));
49
                exit(4);
50
        }
51
        write_prep_boot_partition(out_fd);
52
        _LE(0x400, &hdr.entry_point);
53
        _LE(info.st_size+0x400, &hdr.image_length);
54
        lseek(out_fd, 0x200, 0);
55
        if (write(out_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
56
        {
57
                fprintf(stderr, "Can't write output file: %s\n", strerror(errno));
58
                exit(5);
59
        }
60
        lseek(out_fd, 0x400, 0);
61
        while ((len = read(in_fd, buf, sizeof(buf))) > 0)
62
        {
63
                if (write(out_fd, buf, len) != len)
64
                {
65
                        fprintf(stderr, "Can't write output file: %s\n", strerror(errno));
66
                        exit(5);
67
                }
68
        }
69
        if (len < 0)
70
        {
71
                fprintf(stderr, "Can't read input file: %s\n", strerror(errno));
72
                exit(6);
73
        }
74
        close(in_fd);
75
        close(out_fd);
76
}
77
 
78
/* Adapted from IBM Naked Application Package (NAP) */
79
 
80
#define Align(value,boundary)                                           \
81
        (((value) + (boundary) - 1) & ~((boundary) - 1))
82
 
83
#define HiByte(word)            ((word_t)(word) >> 8)
84
#define LoByte(word)            ((word_t)(word) & 0xFF)
85
 
86
#define HiWord(dword)           ((dword_t)(dword) >> 16)
87
#define LoWord(dword)           ((dword_t)(dword) & 0xFFFF)
88
 
89
/*
90
 * Little-endian stuff
91
 */
92
#define LeWord(word)                                                    \
93
        (((word_t)(word) >> 8) | ((word_t)(word) << 8))
94
 
95
#define LeDword(dword)                                                  \
96
        (LeWord(LoWord(dword)) << 16) | LeWord(HiWord(dword))
97
 
98
#define PcDword(dword)                                                  \
99
        (LeWord(LoWord(dword)) << 16) | LeWord(HiWord(dword))
100
 
101
 
102
typedef unsigned long dword_t;
103
typedef unsigned short word_t;
104
typedef unsigned char byte_t;
105
typedef byte_t block_t[512];
106
typedef byte_t page_t[4096];
107
 
108
/*
109
 * Partition table entry
110
 *  - from the PReP spec
111
 */
112
typedef struct partition_entry {
113
    byte_t      boot_indicator;
114
    byte_t      starting_head;
115
    byte_t      starting_sector;
116
    byte_t      starting_cylinder;
117
 
118
    byte_t      system_indicator;
119
    byte_t      ending_head;
120
    byte_t      ending_sector;
121
    byte_t      ending_cylinder;
122
 
123
    dword_t     beginning_sector;
124
    dword_t     number_of_sectors;
125
} partition_entry_t;
126
 
127
#define BootActive      0x80
128
#define SystemPrep      0x41
129
 
130
 
131
/*
132
 * Writes the "boot record", which contains the partition table, to the
133
 * diskette, followed by the dummy PC boot block and load image descriptor
134
 * block.  It returns the number of bytes it has written to the load
135
 * image.
136
 *
137
 * The boot record is the first block of the diskette and identifies the
138
 * "PReP" partition.  The "PReP" partition contains the "load image" starting
139
 * at offset zero within the partition.  The first block of the load image is
140
 * a dummy PC boot block.  The second block is the "load image descriptor"
141
 * which contains the size of the load image and the entry point into the
142
 * image.  The actual boot image starts at offset 1024 bytes (third sector)
143
 * in the partition.
144
 */
145
void
146
write_prep_boot_partition(int out_fd)
147
{
148
    block_t block;
149
    partition_entry_t *pe = (partition_entry_t *)&block[0x1BE];
150
    dword_t *entry  = (dword_t *)&block[0];
151
    dword_t *length = (dword_t *)&block[4];
152
 
153
    bzero( &block, sizeof block );
154
 
155
    /*
156
     * Magic marker
157
     */
158
    block[510] = 0x55;
159
    block[511] = 0xAA;
160
 
161
    /*
162
     * Build a "PReP" partition table entry in the boot record
163
     *  - "PReP" may only look at the system_indicator
164
     */
165
    pe->boot_indicator   = BootActive;
166
    pe->system_indicator = SystemPrep;
167
 
168
    /*
169
     * The first block of the diskette is used by this "boot record" which
170
     * actually contains the partition table. (The first block of the
171
     * partition contains the boot image, but I digress...)  We'll set up
172
     * one partition on the diskette and it shall contain the rest of the
173
     * diskette.
174
     */
175
    pe->starting_head     = 0;           /* zero-based                        */
176
    pe->starting_sector   = 2;          /* one-based                         */
177
    pe->starting_cylinder = 0;           /* zero-based                        */
178
 
179
    pe->ending_head       = 1;          /* assumes two heads                 */
180
    pe->ending_sector     = 18;         /* assumes 18 sectors/track          */
181
    pe->ending_cylinder   = 79;         /* assumes 80 cylinders/diskette     */
182
 
183
    /*
184
     * The "PReP" software ignores the above fields and just looks at
185
     * the next two.
186
     *   - size of the diskette is (assumed to be)
187
     *     (2 tracks/cylinder)(18 sectors/tracks)(80 cylinders/diskette)
188
     *   - unlike the above sector numbers, the beginning sector is zero-based!
189
     */
190
#if 0     
191
    pe->beginning_sector  = LeDword(1);
192
#else
193
    /* This has to be 0 on the PowerStack? */
194
    pe->beginning_sector  = LeDword(0);
195
#endif    
196
    pe->number_of_sectors = LeDword(2*18*80-1);
197
 
198
    /*
199
     * Write the partition table
200
     */
201
    lseek( out_fd, 0, 0 );
202
    write( out_fd, block, sizeof block );
203
}
204
 

powered by: WebSVN 2.1.0

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