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",
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
const char * get(const char *name) const
Get argument by name.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
const String & name() const
Retrieve program name.
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
Size readBootSymbols(const char *file, const char *prefix, Vector< BootEntry * > *entries)
Read boot symbols using a configuration file.
virtual Result exec()
Execute the application.
virtual Result output(const char *string) const
Print text to output.
virtual ~BootImageCreate()
Destructor.
BootImageCreate(int argc, char **argv)
Constructor.
static const Size PageSize
Size of memory pages as supported by this program.
Vectors are dynamically resizeable Arrays.
virtual int insert(const T &item)
Adds the given item to the Vector, if possible.
virtual Size count() const
Returns the number of items inside the Vector.
#define BOOTENTRY_MAX_REGIONS
Maximum memory regions.
#define BOOTIMAGE_NAMELEN
Maximum length of BootSymbol names.
#define BOOTIMAGE_MAGIC0
First magic byte.
#define BOOTIMAGE_REVISION
Version of the boot image layout.
#define BOOTIMAGE_MAGIC1
Second magic byte.
@ BootProgram
Executable program.
@ BootPrivProgram
Privileged executable program.
#define SEEK_SET
Seek relative to start-of-file.
C int strncmp(const char *dest, const char *src, size_t count)
Compare two strings, by only a maximum number of bytes.
C size_t strlen(const char *str)
Calculate the length of a string.
C char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
C 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...
C int errno
The lvalue errno is used by many functions to return error values.
C void exit(int status)
Terminate a process.
C void * memset(void *dest, int ch, size_t count)
Fill memory with a constant byte.
#define EXIT_FAILURE
Unsuccessful termination.
C int printf(const char *format,...)
Output a formatted string to standard output.
C int fclose(FILE *stream)
Close a stream.
C size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream)
Binary input.
C int strncpy(char *dest, const char *src, size_t sz)
Copy a string, given a maximum number of bytes.
C FILE * fopen(const char *filename, const char *mode)
Open a stream.
C int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
#define PATH_MAX
Maximum file path length.
#define NULL
NULL means zero.
unsigned long Address
A memory address.
unsigned int uint
Unsigned integer number.
unsigned int Size
Any sane size indicator cannot go negative.
unsigned char u8
Unsigned 8-bit number.
Executable entry for use inside a boot image.
BootSymbol symbol
BootSymbol definition.
BootImage contains executable programs to be loaded at system bootup.
u32 magic[2]
Magic numbers to detect a valid boot image.
u16 symbolTableCount
Number of entries in the symbols table.
u32 segmentsTableOffset
Offset of the segments table.
u16 segmentsTableCount
Number of entries in the segments table.
u32 symbolTableOffset
Offset of the symbol table.
u8 layoutRevision
Version of the boot image layout.
u32 bootImageSize
Total size of the boot image in bytes.
u32 size
Total size of the segment.
u32 virtualAddress
Virtual memory address to load the segment.
u32 offset
Offset in the boot image of the segment contents.
Program embedded in the BootImage.
u32 segmentsTotalSize
Total size of the BootSymbol segments.
u16 segmentsCount
Number of contiguous entries in the segment table.
u32 entry
Program entry point (only valid for BootProgram symbols).
u32 segmentsOffset
Offset in the segments table.
BootSymbolType type
Type of boot symbol.
A structure containing information about a file.
The <sys/stat.h> header shall define the stat structure.
off_t st_size
For regular files, the file size in bytes.