You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.2 KiB
C
88 lines
2.2 KiB
C
7 years ago
|
/**
|
||
|
* @file initrd.c
|
||
|
* Initrd image support
|
||
|
*
|
||
|
* Copyright (C) 2018 Clyne Sullivan
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
7 years ago
|
/**
|
||
|
* The format of the initrd file is custom; made with the 'rba' utility.
|
||
|
*/
|
||
|
|
||
7 years ago
|
#include <initrd.h>
|
||
7 years ago
|
#include <heap.h>
|
||
7 years ago
|
#include <string.h>
|
||
7 years ago
|
|
||
|
extern uint8_t _binary_initrd_img_start[];
|
||
|
extern uint8_t _binary_initrd_img_size[];
|
||
|
|
||
7 years ago
|
static const uint8_t *initrd_start = (uint8_t *)_binary_initrd_img_start;
|
||
7 years ago
|
static const uint32_t initrd_size = (uint32_t)_binary_initrd_img_size;
|
||
|
|
||
|
uint8_t initrd_validate(void)
|
||
|
{
|
||
7 years ago
|
return 1; // TODO maybe add header/signature to archiver?
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
char *initrd_getfile(uint32_t offset)
|
||
7 years ago
|
{
|
||
7 years ago
|
char *ptr = initrd_start;
|
||
|
for (uint32_t i = 0; i < offset; i++) {
|
||
|
uint32_t len = *((uint32_t *)ptr);
|
||
|
uint32_t datalen = *((uint32_t *)(ptr + 4 + len));
|
||
|
ptr += len + datalen + 8;
|
||
|
if (ptr >= (char *)(initrd_start + initrd_size))
|
||
7 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
7 years ago
|
return ptr;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
char *initrd_getname(uint32_t offset)
|
||
|
{
|
||
|
char *file = initrd_getfile(offset);
|
||
|
if (file == 0)
|
||
|
return 0;
|
||
|
uint32_t len = *((uint32_t *)file);
|
||
|
char *buf = malloc(len + 1);
|
||
|
strncpy(buf, file + 4, len);
|
||
|
buf[len] = '\0';
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
7 years ago
|
char *initrd_readfile(const char *name)
|
||
7 years ago
|
{
|
||
7 years ago
|
char *ptr;
|
||
|
for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
|
||
|
uint32_t len = *((uint32_t *)ptr);
|
||
|
if (!strncmp(name, ptr + 4, len))
|
||
|
return ptr + len + 8;
|
||
7 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
7 years ago
|
uint32_t initrd_filesize(const char *name)
|
||
7 years ago
|
{
|
||
7 years ago
|
char *ptr;
|
||
|
for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
|
||
|
uint32_t len = *((uint32_t *)ptr);
|
||
|
if (!strncmp(name, ptr + 4, len))
|
||
|
return *((uint32_t *)(ptr + len + 4));
|
||
7 years ago
|
}
|
||
7 years ago
|
return 0;
|
||
7 years ago
|
}
|
||
|
|