Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * file_media.c -
 *
 * Written by Eryk Vershen
 */

/*
 * Copyright 1997,1998 by Apple Computer, Inc.
 *              All Rights Reserved 
 *  
 * Permission to use, copy, modify, and distribute this software and 
 * its documentation for any purpose and without fee is hereby granted, 
 * provided that the above copyright notice appears in all copies and 
 * that both the copyright notice and this permission notice appear in 
 * supporting documentation. 
 *  
 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE. 
 *  
 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 */

// for printf()
#include <stdio.h>
// for malloc() & free()
#include <stdlib.h>
// for lseek(), read(), write(), close()
#include <unistd.h>
// for open()
#include <fcntl.h>
// for LONG_MAX
#include <limits.h>
// for errno
#include <errno.h>

#ifdef __linux__
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <linux/hdreg.h>
#include <sys/stat.h>
#else
#ifdef __unix__
#include <sys/ioctl.h>
#include <sys/stat.h>
#endif
#endif

#include "file_media.h"
#include "errors.h"


/*
 * Defines
 */
#ifdef __linux__
#define LOFF_MAX 9223372036854775807LL
extern __loff_t llseek __P ((int __fd, __loff_t __offset, int __whence));
#elif defined(__NetBSD__) || defined(__APPLE__)
#define loff_t off_t
#define llseek lseek
#define LOFF_MAX LLONG_MAX
#else
#define loff_t long
#define llseek lseek
#define LOFF_MAX LONG_MAX
#endif


/*
 * Types
 */
typedef struct file_media *FILE_MEDIA;

struct file_media {
    struct media	m;
    int			fd;
    int			regular_file;
};

struct file_media_globals {
    long		exists;
    long		kind;
};

typedef struct file_media_iterator *FILE_MEDIA_ITERATOR;

struct file_media_iterator {
    struct media_iterator   m;
    long		    style;
    long		    index;
};


/*
 * Global Constants
 */
int potential_block_sizes[] = {
    1, 512, 1024, 2048, 4096, 8192, 16834,
    0
};

enum {
    kSCSI_Disks = 0,
    kATA_Devices = 1,
    kSCSI_CDs = 2,
    kMaxStyle = 2
};


/*
 * Global Variables
 */
static long file_inited = 0;
static struct file_media_globals file_info;

/*
 * Forward declarations
 */
int compute_block_size(int fd);
void file_init(void);
FILE_MEDIA new_file_media(void);
long read_file_media(MEDIA m, long long offset, uint32_t count, void *address);
long write_file_media(MEDIA m, long long offset, uint32_t count, void *address);
long close_file_media(MEDIA m);
long os_reload_file_media(MEDIA m);
FILE_MEDIA_ITERATOR new_file_iterator(void);
void reset_file_iterator(MEDIA_ITERATOR m);
char *step_file_iterator(MEDIA_ITERATOR m);
void delete_file_iterator(MEDIA_ITERATOR m);


/*
 * Routines
 */
void
file_init(void)
{
    if (file_inited != 0) {
	return;
    }
    file_inited = 1;
    
    file_info.kind = allocate_media_kind();
}


FILE_MEDIA
new_file_media(void)
{
    return (FILE_MEDIA) new_media(sizeof(struct file_media));
}


int
compute_block_size(int fd)
{
    int size;
    int max_size;
    loff_t x;
    long t;
    int i;
    char *buffer;
    
    max_size = 0;
    for (i = 0; ; i++) {
    	size = potential_block_sizes[i];
    	if (size == 0) {
	    break;
    	}
    	if (max_size < size) {
	    max_size = size;
    	}
    }
    
    buffer = malloc(max_size);
    if (buffer != 0) {
	for (i = 0; ; i++) {
	    size = potential_block_sizes[i];
	    if (size == 0) {
		break;
	    }
	    if ((x = llseek(fd, (loff_t)0, 0)) < 0) {
		error(errno, "Can't seek on file");
		break;
	    }
	    if ((t = read(fd, buffer, size)) == size) {
		free(buffer);
		return size;
	    }
	}
    }
    return 0;
}


MEDIA
open_file_as_media(char *file, int oflag)
{
    FILE_MEDIA	a;
    int			fd;
    loff_t off;
#if defined(__linux__) || defined(__unix__)
    struct stat info;
#endif
	
    if (file_inited == 0) {
	    file_init();
    }
    
    a = 0;
    fd = open(file, oflag);
    if (fd >= 0) {
	a = new_file_media();
	if (a != 0) {
	    a->m.kind = file_info.kind;
	    a->m.grain = compute_block_size(fd);
	    off = llseek(fd, (loff_t)0, 2);	/* seek to end of media */
#if !defined(__linux__) && !defined(__unix__)
	    if (off <= 0) {
		off = 1; /* XXX not right? */
	    }
#endif
	    //printf("file size = %Ld\n", off);
	    a->m.size_in_bytes = (long long) off;
	    a->m.do_read = read_file_media;
	    a->m.do_write = write_file_media;
	    a->m.do_close = close_file_media;
	    a->m.do_os_reload = os_reload_file_media;
	    a->fd = fd;
	    a->regular_file = 0;
#if defined(__linux__) || defined(__unix__)
	    if (fstat(fd, &info) < 0) {
		error(errno, "can't stat file '%s'", file);
	    } else {
		a->regular_file = S_ISREG(info.st_mode);
	    }
#endif
	} else {
	    close(fd);
	}
    }
    return (MEDIA) a;
}


long
read_file_media(MEDIA m, long long offset, uint32_t count, void *address)
{
    FILE_MEDIA a;
    long rtn_value;
    loff_t off;
    int t;

    a = (FILE_MEDIA) m;
    rtn_value = 0;
    if (a == 0) {
	/* no media */
	fprintf(stderr,"no media\n");
    } else if (a->m.kind != file_info.kind) {
	/* wrong kind - XXX need to error here - this is an internal problem */
	fprintf(stderr,"wrong kind\n");
    } else if (count <= 0 || count % a->m.grain != 0) {
	/* can't handle size */
	fprintf(stderr,"bad size\n");
    } else if (offset < 0 || offset % a->m.grain != 0) {
	/* can't handle offset */
	fprintf(stderr,"bad offset\n");
    } else if (offset + (long long) count > a->m.size_in_bytes && a->m.size_in_bytes != (long long) 0) {
	/* check for offset (and offset+count) too large */
	fprintf(stderr,"offset+count too large\n");
    } else if (offset + count > (long long) LOFF_MAX) {
	/* check for offset (and offset+count) too large */
	fprintf(stderr,"offset+count too large 2\n");
    } else {
	/* do the read */
	off = offset;
	if ((off = llseek(a->fd, off, 0)) >= 0) {
	    if ((t = read(a->fd, address, count)) == (ssize_t)count) {
		rtn_value = 1;
	    } else {
		fprintf(stderr,"read failed\n");
	    }
	} else {
	    fprintf(stderr,"lseek failed\n");
	}
    }
    return rtn_value;
}


long
write_file_media(MEDIA m, long long offset, uint32_t count, void *address)
{
    FILE_MEDIA a;
    long rtn_value;
    loff_t off;
    int t;
	
    a = (FILE_MEDIA) m;
    rtn_value = 0;
    if (a == 0) {
	/* no media */
    } else if (a->m.kind != file_info.kind) {
	/* wrong kind - XXX need to error here - this is an internal problem */
    } else if (count <= 0 || count % a->m.grain != 0) {
	/* can't handle size */
    } else if (offset < 0 || offset % a->m.grain != 0) {
	/* can't handle offset */
    } else if (offset + count > (long long) LOFF_MAX) {
	/* check for offset (and offset+count) too large */
    } else {
	/* do the write  */
	off = offset;
	if ((off = llseek(a->fd, off, 0)) >= 0) {
		if ((t = write(a->fd, address, count)) == (ssize_t)count) {
		if (off + (long long) count > a->m.size_in_bytes) {
			a->m.size_in_bytes = off + count;
		}
		rtn_value = 1;
	    }
	}
    }
    return rtn_value;
}


long
close_file_media(MEDIA m)
{
    FILE_MEDIA a;
    
    a = (FILE_MEDIA) m;
    if (a == 0) {
	return 0;
    } else if (a->m.kind != file_info.kind) {
	/* XXX need to error here - this is an internal problem */
	return 0;
    }
    
    close(a->fd);
    return 1;
}


long
os_reload_file_media(MEDIA m)
{
    FILE_MEDIA a;
    long rtn_value;
#if defined(__linux__)
    int i;
    int saved_errno;
#endif
	
    a = (FILE_MEDIA) m;
    rtn_value = 0;
    if (a == 0) {
	/* no media */
    } else if (a->m.kind != file_info.kind) {
	/* wrong kind - XXX need to error here - this is an internal problem */
    } else if (a->regular_file) {
	/* okay - nothing to do */
	rtn_value = 1;
    } else {
#ifdef __linux__
	sync();
	sleep(2);
	if ((i = ioctl(a->fd, BLKRRPART)) != 0) {
	    saved_errno = errno;
	} else {
	    // some kernel versions (1.2.x) seem to have trouble
	    // rereading the partition table, but if asked to do it
	    // twice, the second time works. - biro@yggdrasil.com */
	    sync();
	    sleep(2);
	    if ((i = ioctl(a->fd, BLKRRPART)) != 0) {
		saved_errno = errno;
	    }
	}

	// printf("Syncing disks.\n");
	sync();
	sleep(4);		/* for sync() */

	if (i < 0) {
	    error(saved_errno, "Re-read of partition table failed");
	    printf("Reboot your system to ensure the "
		    "partition table is updated.\n");
	}
#endif
	rtn_value = 1;
    }
    return rtn_value;
}


#if !defined(__linux__) && !defined(__unix__)
#pragma mark -
#endif


FILE_MEDIA_ITERATOR
new_file_iterator(void)
{
    return (FILE_MEDIA_ITERATOR) new_media_iterator(sizeof(struct file_media_iterator));
}


MEDIA_ITERATOR
create_file_iterator(void)
{
    FILE_MEDIA_ITERATOR a;
    
    if (file_inited == 0) {
	file_init();
    }
    
    a = new_file_iterator();
    if (a != 0) {
	a->m.kind = file_info.kind;
	a->m.state = kInit;
	a->m.do_reset = reset_file_iterator;
	a->m.do_step = step_file_iterator;
	a->m.do_delete = delete_file_iterator;
	a->style = 0;
	a->index = 0;
    }

    return (MEDIA_ITERATOR) a;
}


void
reset_file_iterator(MEDIA_ITERATOR m)
{
    FILE_MEDIA_ITERATOR a;
    
    a = (FILE_MEDIA_ITERATOR) m;
    if (a == 0) {
	/* no media */
    } else if (a->m.kind != file_info.kind) {
	/* wrong kind - XXX need to error here - this is an internal problem */
    } else if (a->m.state != kInit) {
	a->m.state = kReset;
    }
}


char *
step_file_iterator(MEDIA_ITERATOR m)
{
    FILE_MEDIA_ITERATOR a;
    char *result;
    struct stat info;
    int	fd;
    int bump;
    int value;
    
    a = (FILE_MEDIA_ITERATOR) m;
    if (a == 0) {
	/* no media */
    } else if (a->m.kind != file_info.kind) {
	/* wrong kind - XXX need to error here - this is an internal problem */
    } else {
	switch (a->m.state) {
	case kInit:
	    a->m.state = kReset;
	    /* fall through to reset */
	case kReset:
	    a->style = 0 /* first style */;
	    a->index = 0 /* first index */;
	    a->m.state = kIterating;
	    /* fall through to iterate */
	case kIterating:
	    while (1) {
		if (a->style > kMaxStyle) {
		    break;
		}
#ifndef notdef
		/* if old version of mklinux then skip CD drive */
		if (a->style == kSCSI_Disks && a->index == 3) {
		    a->index += 1;
		}
#endif
		/* generate result */
		result = (char *) malloc(20);
		if (result != NULL) {
		    /*
		     * for DR3 we should actually iterate through:
		     *
		     *    /dev/sd[a...]    # first missing is end of list
		     *    /dev/hd[a...]    # may be holes in sequence
		     *    /dev/scd[0...]   # first missing is end of list
		     *
		     * and stop in each group when either a stat of
		     * the name fails or if an open fails for
		     * particular reasons.
		     */
		    bump = 0;
		    value = (int) a->index;
		    switch (a->style) {
		    case kSCSI_Disks:
			if (value < 26) {
			    snprintf(result, 20, "/dev/sd%c", 'a'+value);
			} else if (value < 676) {
			    snprintf(result, 20, "/dev/sd%c%c",
				    'a' + value / 26,
				    'a' + value % 26);
			} else {
			    bump = -1;
			}
			break;
		    case kATA_Devices:
			if (value < 26) {
			    snprintf(result, 20, "/dev/hd%c", 'a'+value);
			} else {
			    bump = -1;
			}
			break;
		    case kSCSI_CDs:
			if (value < 10) {
			    snprintf(result, 20, "/dev/scd%c", '0'+value);
			} else {
			    bump = -1;
			}
			break;
		    }
		    if (bump != 0) {
			// already set don't even check
		    } else if (stat(result, &info) < 0) {
			bump = 1;
		    } else if ((fd = open(result, O_RDONLY)) >= 0) {
			close(fd);
#if defined(__linux__) || defined(__unix__)
		    } else if (errno == ENXIO || errno == ENODEV) {
			if (a->style == kATA_Devices) {
			    bump = -1;
			} else {
			    bump = 1;
			}
#endif
		    }
		    if (bump) {
			if (bump > 0) {
			    a->style += 1; /* next style */
			    a->index = 0; /* first index again */
			} else {
			    a->index += 1; /* next index */
			}
			free(result);
			continue;
		    }
		}

		a->index += 1; /* next index */
		return result;
	    }
	    a->m.state = kEnd;
	    /* fall through to end */
	case kEnd:
	default:
	    break;
	}
    }
    return 0 /* no entry */;
}


void
delete_file_iterator(MEDIA_ITERATOR m)
{
    return;
}