~ubuntu-branches/ubuntu/precise/silo/precise

« back to all changes in this revision

Viewing changes to silo/silocheck.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2007-10-25 09:28:08 UTC
  • mfrom: (15.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071025092808-1yhj12t7s4zqsfu5
Tags: 1.4.13a+git20070930-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build with -fno-stack-protector.
  - Change silo.postinst to automatically update the boot block without
    invoking siloconfig and keep asking questions on upgrades.
  - Convert silo.conf to use /dev/disk/by-uuid.
  - Ubuntu maintainer foobar.
  - Fix debian/rules call to dh_installdocs.
  - Drop the requirement of gcc-4.1 and start using default gcc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* silocheck - checks whether a file is located below 1GB, so that
 
2
   SILO can load it
 
3
   
 
4
   Copyright (C) 1996 Jakub Jelinek
 
5
   
 
6
   This program is free software; you can redistribute it and/or modify
 
7
   it under the terms of the GNU General Public License as published by
 
8
   the Free Software Foundation; either version 2 of the License, or
 
9
   (at your option) any later version.
 
10
   
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with this program; if not, write to the Free Software
 
18
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 
19
   USA.  */
 
20
 
 
21
#ifdef __sun__
 
22
#include "../second/fs/ufs.c"
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdarg.h>
 
27
#include <string.h>
 
28
#include <stdlib.h>
 
29
#include <unistd.h>
 
30
#include <sys/ioctl.h>
 
31
#ifdef __GLIBC__
 
32
#  include <sys/stat.h>
 
33
#  define _LINUX_STAT_H
 
34
#  define _LINUX_TIME_H
 
35
#  define _LINUX_STRING_H_
 
36
#endif
 
37
#ifdef __linux__
 
38
#  include <linux/fs.h>
 
39
#  include <ext2fs/ext2_fs.h>
 
40
#  include <ext2fs/ext2fs.h>
 
41
#  include <scsi/scsi.h>
 
42
#  include <endian.h>
 
43
#  ifdef __GLIBC__
 
44
#    include <sys/sysmacros.h>
 
45
#    define mmajor(x) major(x)
 
46
#    define mminor(x) minor(x)
 
47
#    define mmakedev(x,y) makedev(x,y)
 
48
#  else
 
49
#    define mmajor(x) (int)((x) >> 8)
 
50
#    define mminor(x) (int)((x) & 0xff)
 
51
#    define mmakedev(major, minor) (((major) << 8) | (minor))
 
52
#  endif
 
53
#elif defined (__sun__)
 
54
#  include <sys/types.h>
 
55
#  include <sys/stat.h>
 
56
#  include <non-linux/ext2_fs.h>
 
57
#  include <ext2fs/ext2fs.h>
 
58
#  include "ufs.h"
 
59
#  include <limits.h>
 
60
#  include <sys/byteorder.h>
 
61
#  ifdef _BIG_ENDIAN
 
62
#    define BYTE_ORDER 4321
 
63
#  elif defined (_LITTLE_ENDIAN)
 
64
#    define BYTE_ORDER 1234
 
65
#  else
 
66
#    error "Unknown byteorder"
 
67
#  endif
 
68
static int ufs_blocks (char *, ino_t);
 
69
#else
 
70
#  error "Unknown system"
 
71
#endif
 
72
#include <fcntl.h>
 
73
#include <sys/stat.h>
 
74
#include <dirent.h>
 
75
#include <ctype.h>
 
76
#ifndef __intel__
 
77
#include "prom.h"
 
78
#endif
 
79
 
 
80
/* This is just so that we don't have to fight with incompatible ufs_fs.h headers */
 
81
#define SILO_UFS_MAGIC 0x00011954
 
82
struct silo_ufs_super_block {
 
83
        unsigned char xxx1[36];
 
84
        unsigned int fs_fsize;
 
85
        unsigned char xxx2[1332];
 
86
        unsigned int fs_magic;
 
87
};
 
88
                                
 
89
struct sun_disklabel {
 
90
    unsigned char info[128];    /* Informative text string */
 
91
    unsigned char spare[292];   /* Boot information etc. */
 
92
    unsigned short rspeed;      /* Disk rotational speed */
 
93
    unsigned short pcylcount;   /* Physical cylinder count */
 
94
    unsigned short sparecyl;    /* extra sects per cylinder */
 
95
    unsigned char spare2[4];    /* More magic... */
 
96
    unsigned short ilfact;      /* Interleave factor */
 
97
    unsigned short ncyl;        /* Data cylinder count */
 
98
    unsigned short nacyl;       /* Alt. cylinder count */
 
99
    unsigned short ntrks;       /* Tracks per cylinder */
 
100
    unsigned short nsect;       /* Sectors per track */
 
101
    unsigned char spare3[4];    /* Even more magic... */
 
102
    struct sun_partition {
 
103
        unsigned long start_cylinder;
 
104
        unsigned long num_sectors;
 
105
    } partitions[8];
 
106
    unsigned short magic;       /* Magic number */
 
107
    unsigned short csum;        /* Label xor'd checksum */
 
108
};
 
109
 
 
110
#if BYTE_ORDER == 4321
 
111
__u32 swab32 (__u32 value)
 
112
{
 
113
    return ((value >> 24) | ((value >> 8) & 0xff00) |
 
114
            ((value << 8) & 0xff0000) | (value << 24));
 
115
}
 
116
 
 
117
__u16 swab16 (__u16 value)
 
118
{
 
119
    return (value >> 8) | (value << 8);
 
120
}
 
121
#define bswab32(x) (x)
 
122
#define bswab16(x) (x)
 
123
#else
 
124
#define swab32(x) (x)
 
125
#define swab16(x) (x)
 
126
__u32 bswab32 (__u32 value)
 
127
{
 
128
    return ((value >> 24) | ((value >> 8) & 0xff00) |
 
129
            ((value << 8) & 0xff0000) | (value << 24));
 
130
}
 
131
 
 
132
__u16 bswab16 (__u16 value)
 
133
{
 
134
    return (value >> 8) | (value << 8);
 
135
}
 
136
#endif
 
137
 
 
138
unsigned char nsect;
 
139
unsigned short bs;
 
140
unsigned long doff;
 
141
int nblocks = 0;
 
142
__u32 blocks[16384];
 
143
 
 
144
static void silo_fatal(char *fmt,...)
 
145
{
 
146
    va_list ap;
 
147
    va_start (ap, fmt);
 
148
    fprintf (stderr, "Fatal error: ");
 
149
    vfprintf (stderr, fmt, ap);
 
150
    putc ('\n', stderr);
 
151
    va_end (ap);
 
152
    exit (2);
 
153
}
 
154
 
 
155
int check_fs (int fd)
 
156
{
 
157
    struct silo_ufs_super_block ufs;
 
158
    struct ext2_super_block sb; /* Super Block Info */
 
159
 
 
160
    if (lseek (fd, 1024, 0) != 1024 || read (fd, &sb, sizeof (sb)) != sizeof (sb))
 
161
        silo_fatal("Cannot read Super Block!");
 
162
    if (swab16 (sb.s_magic) == EXT2_SUPER_MAGIC)
 
163
        return 1024 << swab32 (sb.s_log_block_size);
 
164
    if (lseek (fd, 8192, 0) != 8192 || read (fd, &ufs, sizeof (ufs)) != sizeof (ufs))
 
165
        return -1;
 
166
    if (bswab32 (ufs.fs_magic) == SILO_UFS_MAGIC)
 
167
        return ufs.fs_fsize;
 
168
    return -1;
 
169
}
 
170
 
 
171
void read_sb (char *device, char *bootdev)
 
172
{
 
173
    int fd, partno;
 
174
    char buff[512];
 
175
    struct sun_disklabel *sdl;
 
176
    int offset;
 
177
 
 
178
    if ((fd = open (device, O_RDONLY)) == -1)
 
179
        silo_fatal("Cannot open superblock on %s", device);
 
180
    bs = check_fs (fd);
 
181
    if (bs == (unsigned short)-1)
 
182
        silo_fatal("File systems other than ext2, ext3, ufs and romfs "
 
183
                   "not yet supported", device);
 
184
    close (fd);
 
185
    nsect = bs / 512;
 
186
    doff = 0;
 
187
#ifdef __linux__    
 
188
    partno = (int) (*(device + strlen (device) - 1) - '0') - 1;
 
189
#elif defined(__sun__)
 
190
    partno = (int) (*(device + strlen (device) - 1) - '0');
 
191
#endif
 
192
    sdl = (struct sun_disklabel *) &buff;
 
193
    if ((fd = open (bootdev, O_RDONLY)) == -1)
 
194
        silo_fatal("Error opening %s", bootdev);
 
195
    if (read (fd, buff, sizeof (buff)) != sizeof (buff))
 
196
        silo_fatal("Error reading %s's label", bootdev);
 
197
    doff = bswab16(sdl->ntrks) * bswab16(sdl->nsect) * bswab32(sdl->partitions[partno].start_cylinder);
 
198
    offset = bswab16(sdl->ntrks) * bswab16(sdl->nsect) * bswab32(sdl->partitions[0].start_cylinder);
 
199
    close (fd);
 
200
}
 
201
 
 
202
int get_partition_blocks (char *device, char *filename)
 
203
{
 
204
    int fd;
 
205
    int block, i, j, k;
 
206
    struct stat st;
 
207
    int size;
 
208
    
 
209
    if ((fd = open (filename, O_RDONLY)) == -1) {
 
210
        silo_fatal("Cannot find %s", filename);
 
211
    }
 
212
    if (fstat (fd, &st) < 0) {
 
213
        silo_fatal("Couldn't stat %s", filename);
 
214
    }
 
215
#ifdef __linux__
 
216
    size = st.st_size;
 
217
    for (i = 0, j = 0;; i++) {
 
218
        block = i;
 
219
        if ((j << 9) >= size || ioctl (fd, FIBMAP, &block) < 0)
 
220
            break;
 
221
        if (!block) {
 
222
            for (k = 0; k < nsect; k++)
 
223
                blocks[j++] = 0;
 
224
        } else {
 
225
            for (k = 0; k < nsect; k++)
 
226
                blocks[j++] = block * nsect + doff + k;
 
227
        }
 
228
    }
 
229
    blocks[j] = 0;
 
230
    nblocks = j;
 
231
#elif defined(__sun__)
 
232
    ufs_blocks (device, st.st_ino);
 
233
#endif
 
234
    close (fd);
 
235
    return 0;
 
236
}
 
237
 
 
238
char *find_dev(int number)
 
239
{
 
240
#ifdef __linux__
 
241
#  define DEVNAME "/dev"
 
242
#else
 
243
#  define DEVNAME "/dev/dsk"
 
244
#endif
 
245
    DIR *dp;
 
246
    char *p;
 
247
    struct dirent *dir;
 
248
    static char name[PATH_MAX+1];
 
249
    struct stat s;
 
250
    
 
251
    if (!number) return NULL;
 
252
    if ((dp = opendir(DEVNAME)) == NULL) return NULL;
 
253
    strcpy(name,DEVNAME "/");
 
254
    p = strchr (name, 0);
 
255
    while ((dir = readdir(dp)) != NULL) {
 
256
        strcpy(p,dir->d_name);
 
257
        if (stat(name,&s) < 0) return NULL;
 
258
        if (S_ISBLK(s.st_mode) && s.st_rdev == number) return name;
 
259
    }
 
260
    return NULL;
 
261
}
 
262
 
 
263
void usage (char *s)
 
264
{
 
265
    fprintf (stderr,
 
266
            "SILOCHECK\n"
 
267
            "Usage: %s [options] filename\n"
 
268
            "Options:\n"
 
269
            " -v             print disk blocks\n"
 
270
            " -c             print disk blocks in a start-len format\n"
 
271
            ,s);
 
272
    exit (2);
 
273
}
 
274
 
 
275
int main(int argc,char **argv)
 
276
{
 
277
    struct stat st1;
 
278
    int i, j, printblocks = 0;
 
279
    char *name;
 
280
    char bootdev[1024], bootdev2[1024];
 
281
    char *filename;
 
282
 
 
283
    name = *argv++;
 
284
    argc--;
 
285
    while (argc && **argv == '-') {
 
286
        argc--;
 
287
        if (argv[0][2]) usage(name);
 
288
        switch ((*argv++)[1]) {
 
289
            case 'v':
 
290
                printblocks = 1;
 
291
                break;
 
292
            case 'c':
 
293
                printblocks = 2;
 
294
                break;
 
295
            default:
 
296
                usage(name);
 
297
        }
 
298
    }
 
299
    if (argc != 1) usage(name);
 
300
    filename = *argv;
 
301
    if (stat (filename, &st1) < 0)
 
302
        silo_fatal("Cannot open %s", filename);
 
303
#ifdef __linux__
 
304
    if (mmajor(st1.st_dev) == 8) {
 
305
        sprintf (bootdev2, "/dev/sd%c%c", (mminor(st1.st_dev) >> 4) + 'a', (mminor(st1.st_dev) & 0xf) + '0');
 
306
        sprintf (bootdev, "/dev/sd%c", (mminor(st1.st_dev) >> 4) + 'a');
 
307
    } else
 
308
#endif  
 
309
    {
 
310
        char *p = find_dev (st1.st_dev);
 
311
 
 
312
        if (!p)
 
313
          silo_fatal("Couldn't find out what device is %s on", filename);
 
314
        strcpy (bootdev, p);
 
315
        strcpy (bootdev2, p);
 
316
#ifdef __sun__
 
317
        if (strlen (p) == strlen ("/dev/dsk/c0t0d0s0")) {
 
318
            p = strchr (p, 0) - 2;
 
319
            if (*p == 's' && p[1] >= '0' && p[1] <= '7') {
 
320
                p = strchr (bootdev, 0) - 1;
 
321
                *p = '2';
 
322
            }
 
323
        }
 
324
#endif
 
325
    }
 
326
    read_sb (bootdev2, bootdev);
 
327
    get_partition_blocks (bootdev2, filename);
 
328
    if (printblocks) {
 
329
        printf ("%s: ", filename);
 
330
        for (i = 0; i < nblocks; i++) {
 
331
             for (j = 0; !blocks[i] && i < nblocks; i++, j++);
 
332
             if (j) {
 
333
                 if (i) printf (", ");
 
334
                 printf ("%dxHOLE", j);
 
335
                 if (i >= nblocks) break;
 
336
             }
 
337
             if (i) printf (", ");
 
338
             if (printblocks == 2) {
 
339
                for (j = 0; blocks[i + j] && blocks[i + j] == blocks[i] + j && j < nblocks; j++);
 
340
                if (j > 1) {
 
341
                    printf ("%08X(%d)", blocks[i], j);
 
342
                    i += j - 1;
 
343
                    continue;
 
344
                }
 
345
             }
 
346
             printf ("%08X", blocks[i]);
 
347
        }
 
348
        printf ("\n");
 
349
    }
 
350
    for (i = 0; i < nblocks; i++) {
 
351
        if (blocks[i] >= 0x200000) {
 
352
            fprintf (stderr, "%s extends past the 1GB boundary and thus SILO might not load it correctly\n", filename);
 
353
            exit (1);
 
354
        }
 
355
    }
 
356
    exit(0);
 
357
}
 
358
 
 
359
#ifdef __sun__
 
360
 
 
361
static errcode_t std_open (const char *name, int flags, io_channel * channel);
 
362
static errcode_t std_close (io_channel channel);
 
363
static errcode_t std_set_blksize (io_channel channel, int blksize);
 
364
static errcode_t std_read_blk (io_channel channel, unsigned long block, int count, void *data);
 
365
static errcode_t std_write_blk (io_channel channel, unsigned long block, int count, const void *data);
 
366
static errcode_t std_flush (io_channel channel);
 
367
 
 
368
static struct struct_io_manager struct_std_manager =
 
369
{
 
370
    EXT2_ET_MAGIC_IO_MANAGER,
 
371
    "linux I/O Manager",
 
372
    std_open,
 
373
    std_close,
 
374
    std_set_blksize,
 
375
    std_read_blk,
 
376
    std_write_blk,
 
377
    std_flush
 
378
};
 
379
 
 
380
static ufs_filsys fs = NULL;
 
381
static io_manager std_io_manager = &struct_std_manager;
 
382
static int std_fd = 0;
 
383
 
 
384
static unsigned int cbs = 1024;         /* Block Size */
 
385
 
 
386
static errcode_t std_open (const char *name, int flags, io_channel * channel)
 
387
{
 
388
    int partno;
 
389
    io_channel io;
 
390
 
 
391
    if (!name)
 
392
        return EXT2_ET_BAD_DEVICE_NAME;
 
393
    io = (io_channel) malloc (sizeof (struct struct_io_channel));
 
394
    if (!io)
 
395
        return EXT2_ET_BAD_DEVICE_NAME;
 
396
    std_fd = open (name, O_RDONLY);
 
397
    if (std_fd < 0)
 
398
        silo_fatal("Cannot open %s", name);
 
399
    memset (io, 0, sizeof (struct struct_io_channel));
 
400
    io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
 
401
    io->manager = std_io_manager;
 
402
    io->name = (char *) malloc (strlen (name) + 1);
 
403
    strcpy (io->name, name);
 
404
    io->block_size = cbs;
 
405
    io->read_error = 0;
 
406
    io->write_error = 0;
 
407
 
 
408
    *channel = io;
 
409
    return 0;
 
410
}
 
411
 
 
412
static errcode_t std_close (io_channel channel)
 
413
{
 
414
    close (std_fd);
 
415
}
 
416
 
 
417
static errcode_t std_set_blksize (io_channel channel, int blksize)
 
418
{
 
419
    channel->block_size = cbs = blksize;
 
420
}
 
421
 
 
422
static errcode_t std_read_blk (io_channel channel, unsigned long block, int count, void *data)
 
423
{
 
424
    int size;
 
425
 
 
426
    size = (count < 0) ? -count : count * cbs;
 
427
    if (lseek (std_fd, block * cbs, SEEK_SET) != block * cbs)
 
428
        silo_fatal("Cannot seek");
 
429
    if (read (std_fd, data, size) != size)
 
430
        silo_fatal("Read error on block %d", block);
 
431
    return 0;
 
432
}
 
433
 
 
434
static errcode_t std_write_blk (io_channel channel, unsigned long block, int count, const void *data)
 
435
{
 
436
}
 
437
 
 
438
static errcode_t std_flush (io_channel channel)
 
439
{
 
440
}
 
441
 
 
442
static int ufs_block_idx = 0;
 
443
static int ufs_blocks_dump (ufs_filsys fs, blk_t *block, int i, void *private)
 
444
{
 
445
    int j;
 
446
    for (j = 0; j < nsect; j++)
 
447
        blocks [ufs_block_idx++] = *block * nsect + doff + j;
 
448
    return 0;
 
449
}
 
450
 
 
451
static int ufs_blocks (char *device, ino_t inode)
 
452
{
 
453
    if (ufs_open (device, std_io_manager, &fs))
 
454
        silo_fatal("Cannot open ufs filesystem containing second stage");
 
455
    nsect = cbs / 512;
 
456
    if (ufs_block_iterate (fs, inode, ufs_blocks_dump, 0))
 
457
        silo_fatal("Block iterating error on second stage");
 
458
    blocks [ufs_block_idx] = 0;
 
459
    nblocks = ufs_block_idx;
 
460
    return 0;
 
461
}
 
462
 
 
463
#endif