Go to the documentation of this file.
33 parser().
registerFlag(
'p',
"prefix",
"Prefix each entry from the config file with the given path");
54 const char *symbolname = 0;
56 Size totalBytes = 0, totalEntries = 0;
60 size_t prefix_len = prefix ?
strlen(prefix) : 0;
65 fprintf(stderr,
"%s: failed to open `%s': %s\r\n",
71 while (fgets(line,
sizeof(line), fp) !=
NULL)
77 if (
strlen(line) <
sizeof(line) - 1)
81 if (
strncmp(line,
"BootProgram ", 12) == 0) {
83 symbolname = line + 12;
84 }
else if (
strncmp(line,
"BootPrivProgram ", 16) == 0) {
86 symbolname = line + 16;
87 }
else if (
strncmp(line,
"BootData ", 9) == 0) {
89 symbolname = line + 9;
91 fprintf(stderr,
"%s: symbol type unknown in line: %s\r\n",
97 line[
sizeof(line)-1] = 0;
104 snprintf(tmp,
sizeof(tmp),
"%s/%s", prefix, symbolname);
105 strncpy(line, tmp,
sizeof(line));
111 if (
stat(symbolname, &st) == -1)
113 fprintf(stderr,
"%s: failed to stat `%s': %s\r\n",
124 fprintf(stderr,
"%s: failed to open `%s': %s\r\n",
130 fprintf(stderr,
"%s: failed to fread `%s': %s\r\n",
141 fprintf(stderr,
"%s: failed to parse executable image format in `%s': %s\r\n",
148 fprintf(stderr,
"%s: failed to extract memory regions from `%s': %s\r\n",
152 entry->numRegions = num;
161 entry->numRegions = 1;
162 entry->regions[0].virt = 0;
166 entry->regions[0].dataOffset = 0;
168 fprintf(stderr,
"%s: unknown boot symbol type: %d\r\n",
177 for (
Size i = 0; i <
entry->numRegions; i++)
179 printf(
"%s[%u]: vaddr=%x size=%u\r\n",
180 symbolname, i, (
uint)
entry->regions[i].virt,
181 entry->regions[i].memorySize);
182 totalBytes +=
entry->regions[i].memorySize;
189 printf(
"%d entries, %d bytes total\r\n", totalEntries, totalBytes);
200 Size numSegments = 0, dataOffset = 0, lastDataOffset = 0;
210 fprintf(stderr,
"%s: failed to read boot symbols\r\n", prog);
216 memset(&image, 0,
sizeof(image));
232 symbols[i].
type = input[i]->symbol.type;
233 symbols[i].
entry = input[i]->symbol.entry;
237 numSegments += input[i]->numRegions;
255 for (
Size j = 0; j < input[i]->numRegions; j++)
259 segments[segCount].
size = input[i]->regions[j].memorySize;
260 segments[segCount].
offset = dataOffset;
266 dataOffset += segments[i].
size;
267 lastDataOffset = dataOffset;
278 fprintf(stderr,
"%s: failed to open `%s': %s\r\n",
284 if (
fwrite(&image,
sizeof(image), 1, fp) <= 0 ||
288 fprintf(stderr,
"%s: failed to write BootImage headers to `%s': %s\r\n",
297 for (
Size j = 0; j < input[i]->numRegions; j++)
300 if (fseek(fp, segments[symbols[i].segmentsOffset].offset,
303 fprintf(stderr,
"%s: failed to seek to BootSegment contents in `%s': %s\r\n",
309 if (
fwrite(input[i]->data + input[i]->regions[j].dataOffset,
310 input[i]->regions[j].memorySize, 1, fp) <= 0)
312 fprintf(stderr,
"%s: failed to write BootSegment contents to `%s': %s\r\n",
int strncmp(const char *dest, const char *src, size_t count)
Compare two strings, by only a maximum number of bytes.
const char * get(const char *name) const
Get argument by name.
#define EXIT_FAILURE
Unsuccessful termination.
off_t st_size
For regular files, the file size in bytes.
The <sys/stat.h> header shall define the stat structure.
size_t strlen(const char *str)
Calculate the length of a string.
C int errno
The lvalue errno is used by many functions to return error values.
@ BootProgram
Executable program.
struct BootSegment BootSegment
Memory segment.
FILE * fopen(const char *filename, const char *mode)
Open a stream.
@ BootPrivProgram
Privileged executable program.
struct BootSymbol BootSymbol
Program embedded in the BootImage.
BootSymbolType type
Type of boot symbol.
u32 segmentsOffset
Offset in the segments table.
unsigned long Address
A memory address.
#define PATH_MAX
Maximum file path length.
#define BOOTIMAGE_MAGIC0
First magic byte.
const ArgumentContainer & arguments() const
Get program arguments.
void setDescription(const String &desc)
Set program description.
virtual Result output(const char *string) const
Print text to output.
static const Size PageSize
Size of memory pages as supported by this program.
#define BOOTIMAGE_NAMELEN
Maximum length of BootSymbol names.
u16 segmentsTableCount
Number of entries in the segments table.
size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
The fwrite() function shall write, from the array pointed to by ptr, up to nitems elements whose size...
u32 bootImageSize
Total size of the boot image in bytes.
unsigned int uint
Unsigned integer number.
virtual Result exec()
Execute the application.
Executable entry for use inside a boot image.
int printf(const char *format,...)
Output a formatted string to standard output.
u32 segmentsTableOffset
Offset of the segments table.
u32 segmentsTotalSize
Total size of the BootSymbol segments.
u16 symbolTableCount
Number of entries in the symbols table.
virtual Size count() const
Returns the number of items inside the Vector.
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
#define NULL
NULL means zero.
unsigned int Size
Any sane size indicator cannot go negative.
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
const String & name() const
Retrieve program name.
virtual int insert(const T &item)
Adds the given item to the Vector, if possible.
int stat(const char *path, struct stat *buf)
Get file status.
#define BOOTENTRY_MAX_REGIONS
Maximum memory regions.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Program embedded in the BootImage.
BootImageCreate(int argc, char **argv)
Constructor.
ArgumentParser & parser()
Get program arguments parser.
#define SEEK_SET
Seek relative to start-of-file.
u32 virtualAddress
Virtual memory address to load the segment.
unsigned char u8
Unsigned 8-bit number.
u16 segmentsCount
Number of contiguous entries in the segment table.
u8 layoutRevision
Version of the boot image layout.
void * memset(void *dest, int ch, size_t count)
Fill memory with a constant byte.
u32 symbolTableOffset
Offset of the symbol table.
u32 entry
Program entry point (only valid for BootProgram symbols).
struct BootEntry BootEntry
Executable entry for use inside a boot image.
#define BOOTIMAGE_REVISION
Version of the boot image layout.
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream)
Binary input.
struct BootImage BootImage
BootImage contains executable programs to be loaded at system bootup.
BootImage contains executable programs to be loaded at system bootup.
u32 size
Total size of the segment.
A structure containing information about a file.
int fclose(FILE *stream)
Close a stream.
int strncpy(char *dest, const char *src, size_t sz)
Copy a string, given a maximum number of bytes.
u32 offset
Offset in the boot image of the segment contents.
u32 magic[2]
Magic numbers to detect a valid boot image.
C void exit(int status)
Terminate a process.
Vectors are dynamically resizeable Arrays.
virtual ~BootImageCreate()
Destructor.
#define BOOTIMAGE_MAGIC1
Second magic byte.
int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
Size readBootSymbols(const char *file, const char *prefix, Vector< BootEntry * > *entries)
Read boot symbols using a configuration file.