2012-03-23 14:36:51 -04:00
|
|
|
/*******************************************************************************
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix 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.
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix 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.
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
initrd.cpp
|
|
|
|
Provides low-level access to a Sortix init ramdisk.
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
*******************************************************************************/
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/platform.h>
|
2013-02-09 12:32:45 -05:00
|
|
|
#include <sortix/kernel/addralloc.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/kernel/vnode.h>
|
|
|
|
#include <sortix/kernel/descriptor.h>
|
2012-03-23 14:36:51 -04:00
|
|
|
#include <sortix/kernel/memorymanagement.h>
|
2012-09-22 16:24:03 -04:00
|
|
|
#include <sortix/kernel/crc32.h>
|
2012-09-23 07:27:42 -04:00
|
|
|
#include <sortix/kernel/string.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/kernel/ioctx.h>
|
|
|
|
#include <sortix/kernel/fsfunc.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
#include <sortix/kernel/syscall.h>
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/fcntl.h>
|
|
|
|
#include <sortix/initrd.h>
|
2012-03-23 14:36:51 -04:00
|
|
|
#include <sortix/stat.h>
|
2012-03-21 12:19:26 -04:00
|
|
|
#include <sortix/mman.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <assert.h>
|
2012-09-22 10:44:50 -04:00
|
|
|
#include <errno.h>
|
2012-09-22 14:38:34 -04:00
|
|
|
#include <string.h>
|
2013-03-22 15:19:45 -04:00
|
|
|
#include <timespec.h>
|
2013-01-08 18:41:35 -05:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
#include "initrd.h"
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
namespace Sortix {
|
|
|
|
namespace InitRD {
|
|
|
|
|
2013-02-09 12:32:45 -05:00
|
|
|
addralloc_t initrd_addr_alloc;
|
2012-08-07 18:19:44 -04:00
|
|
|
uint8_t* initrd = NULL;
|
2012-03-23 14:36:51 -04:00
|
|
|
size_t initrdsize;
|
|
|
|
const initrd_superblock_t* sb;
|
|
|
|
|
2013-06-26 11:14:07 -04:00
|
|
|
__attribute__((unused))
|
2012-03-23 14:36:51 -04:00
|
|
|
static uint32_t HostModeToInitRD(mode_t mode)
|
2011-08-27 11:41:35 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t result = mode & 0777; // Lower 9 bits per POSIX and tradition.
|
|
|
|
if ( S_ISVTX & mode ) { result |= INITRD_S_ISVTX; }
|
|
|
|
if ( S_ISSOCK(mode) ) { result |= INITRD_S_IFSOCK; }
|
|
|
|
if ( S_ISLNK(mode) ) { result |= INITRD_S_IFLNK; }
|
|
|
|
if ( S_ISREG(mode) ) { result |= INITRD_S_IFREG; }
|
|
|
|
if ( S_ISBLK(mode) ) { result |= INITRD_S_IFBLK; }
|
|
|
|
if ( S_ISDIR(mode) ) { result |= INITRD_S_IFDIR; }
|
|
|
|
if ( S_ISCHR(mode) ) { result |= INITRD_S_IFCHR; }
|
|
|
|
if ( S_ISFIFO(mode) ) { result |= INITRD_S_IFIFO; }
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-26 11:14:07 -04:00
|
|
|
__attribute__((unused))
|
2012-03-23 14:36:51 -04:00
|
|
|
static mode_t InitRDModeToHost(uint32_t mode)
|
|
|
|
{
|
|
|
|
mode_t result = mode & 0777; // Lower 9 bits per POSIX and tradition.
|
|
|
|
if ( INITRD_S_ISVTX & mode ) { result |= S_ISVTX; }
|
|
|
|
if ( INITRD_S_ISSOCK(mode) ) { result |= S_IFSOCK; }
|
|
|
|
if ( INITRD_S_ISLNK(mode) ) { result |= S_IFLNK; }
|
|
|
|
if ( INITRD_S_ISREG(mode) ) { result |= S_IFREG; }
|
|
|
|
if ( INITRD_S_ISBLK(mode) ) { result |= S_IFBLK; }
|
|
|
|
if ( INITRD_S_ISDIR(mode) ) { result |= S_IFDIR; }
|
|
|
|
if ( INITRD_S_ISCHR(mode) ) { result |= S_IFCHR; }
|
|
|
|
if ( INITRD_S_ISFIFO(mode) ) { result |= S_IFIFO; }
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Root()
|
|
|
|
{
|
|
|
|
return sb->root;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const initrd_inode_t* GetInode(uint32_t inode)
|
|
|
|
{
|
2012-09-22 10:44:50 -04:00
|
|
|
if ( sb->inodecount <= inode ) { errno = EINVAL; return NULL; }
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t pos = sb->inodeoffset + sb->inodesize * inode;
|
|
|
|
return (const initrd_inode_t*) (initrd + pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stat(uint32_t ino, struct stat* st)
|
|
|
|
{
|
|
|
|
const initrd_inode_t* inode = GetInode(ino);
|
|
|
|
if ( !inode ) { return false; }
|
|
|
|
st->st_ino = ino;
|
|
|
|
st->st_mode = HostModeToInitRD(inode->mode);
|
|
|
|
st->st_nlink = inode->nlink;
|
|
|
|
st->st_uid = inode->uid;
|
|
|
|
st->st_gid = inode->gid;
|
|
|
|
st->st_size = inode->size;
|
2013-03-22 15:19:45 -04:00
|
|
|
st->st_atim = timespec_make(inode->mtime, 0);
|
|
|
|
st->st_ctim = timespec_make(inode->ctime, 0);
|
|
|
|
st->st_mtim = timespec_make(inode->mtime, 0);
|
2012-03-23 14:36:51 -04:00
|
|
|
st->st_blksize = 1;
|
|
|
|
st->st_blocks = inode->size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* Open(uint32_t ino, size_t* size)
|
|
|
|
{
|
|
|
|
const initrd_inode_t* inode = GetInode(ino);
|
|
|
|
if ( !inode ) { return NULL; }
|
|
|
|
*size = inode->size;
|
|
|
|
return initrd + inode->dataoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Traverse(uint32_t ino, const char* name)
|
|
|
|
{
|
|
|
|
const initrd_inode_t* inode = GetInode(ino);
|
|
|
|
if ( !inode ) { return 0; }
|
2012-09-22 10:44:50 -04:00
|
|
|
if ( !INITRD_S_ISDIR(inode->mode) ) { errno = ENOTDIR; return 0; }
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t offset = 0;
|
|
|
|
while ( offset < inode->size )
|
2011-08-27 11:41:35 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t pos = inode->dataoffset + offset;
|
|
|
|
const initrd_dirent* dirent = (const initrd_dirent*) (initrd + pos);
|
2012-09-22 16:53:11 -04:00
|
|
|
if ( dirent->namelen && !strcmp(dirent->name, name) )
|
2011-11-03 13:26:43 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
return dirent->inode;
|
2011-11-20 18:02:53 -05:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
offset += dirent->reclen;
|
|
|
|
}
|
2012-09-22 10:44:50 -04:00
|
|
|
errno = ENOENT;
|
2012-03-23 14:36:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2011-11-20 18:02:53 -05:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
const char* GetFilename(uint32_t dir, size_t index)
|
|
|
|
{
|
|
|
|
const initrd_inode_t* inode = GetInode(dir);
|
|
|
|
if ( !inode ) { return 0; }
|
2012-09-22 10:44:50 -04:00
|
|
|
if ( !INITRD_S_ISDIR(inode->mode) ) { errno = ENOTDIR; return 0; }
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t offset = 0;
|
|
|
|
while ( offset < inode->size )
|
|
|
|
{
|
|
|
|
uint32_t pos = inode->dataoffset + offset;
|
|
|
|
const initrd_dirent* dirent = (const initrd_dirent*) (initrd + pos);
|
|
|
|
if ( index-- == 0 ) { return dirent->name; }
|
|
|
|
offset += dirent->reclen;
|
|
|
|
}
|
2012-09-22 10:44:50 -04:00
|
|
|
errno = EINVAL;
|
2012-03-23 14:36:51 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetNumFiles(uint32_t dir)
|
|
|
|
{
|
|
|
|
const initrd_inode_t* inode = GetInode(dir);
|
|
|
|
if ( !inode ) { return 0; }
|
2012-09-22 10:44:50 -04:00
|
|
|
if ( !INITRD_S_ISDIR(inode->mode) ) { errno = ENOTDIR; return 0; }
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t offset = 0;
|
|
|
|
size_t numentries = 0;
|
|
|
|
while ( offset < inode->size )
|
|
|
|
{
|
|
|
|
uint32_t pos = inode->dataoffset + offset;
|
|
|
|
const initrd_dirent* dirent = (const initrd_dirent*) (initrd + pos);
|
|
|
|
numentries++;
|
|
|
|
offset += dirent->reclen;
|
|
|
|
}
|
|
|
|
return numentries;
|
|
|
|
}
|
2011-12-05 13:36:15 -05:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
void CheckSum()
|
|
|
|
{
|
|
|
|
uint32_t amount = sb->fssize - sb->sumsize;
|
|
|
|
uint8_t* filesum = initrd + amount;
|
|
|
|
if ( sb->sumalgorithm != INITRD_ALGO_CRC32 )
|
|
|
|
{
|
|
|
|
Log::PrintF("Warning: InitRD checksum algorithm not supported\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t crc32 = *((uint32_t*) filesum);
|
|
|
|
uint32_t filecrc32 = CRC32::Hash(initrd, amount);
|
2012-08-01 13:02:24 -04:00
|
|
|
uint32_t doublecheck = CRC32::Hash(initrd, amount);
|
|
|
|
if ( filecrc32 != doublecheck )
|
|
|
|
{
|
|
|
|
// Yes, this has happened. This seems like the goto place for such bugs
|
|
|
|
// to trigger, so I added a more accurate warning.
|
|
|
|
Panic("Calculating InitRD checksum two times gave different results: "
|
|
|
|
"this likely means the kernel have a corruption bug, possibly "
|
|
|
|
"caused by building libc and kernel with different settings or "
|
|
|
|
"a bug in the scheduler/interrupt handler or who knows. "
|
|
|
|
"It is also possible that the laws of logic has changed.");
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( crc32 != filecrc32 )
|
|
|
|
{
|
|
|
|
PanicF("InitRD had checksum %X, expected %X: this means the ramdisk "
|
|
|
|
"may have been corrupted by the bootloader.", filecrc32, crc32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(addr_t phys, size_t size)
|
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
assert(!initrd);
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-02-09 12:32:45 -05:00
|
|
|
// Allocate the needed kernel virtual address space.
|
|
|
|
if ( !AllocateKernelAddress(&initrd_addr_alloc, size) )
|
|
|
|
PanicF("Can't allocate 0x%zx bytes of kernel address space for the "
|
|
|
|
"init ramdisk", size );
|
|
|
|
|
|
|
|
// Map the physical frames onto our address space.
|
|
|
|
addr_t mapat = initrd_addr_alloc.from;
|
|
|
|
for ( size_t i = 0; i < size; i += Page::Size() )
|
|
|
|
if ( !Memory::Map(phys + i, mapat + i, PROT_KREAD) )
|
|
|
|
Panic("Unable to map the init ramdisk into virtual memory");
|
2012-03-23 14:36:51 -04:00
|
|
|
Memory::Flush();
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2013-02-09 12:32:45 -05:00
|
|
|
initrd = (uint8_t*) mapat;
|
2012-03-23 14:36:51 -04:00
|
|
|
initrdsize = size;
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( size < sizeof(*sb) ) { PanicF("initrd is too small"); }
|
|
|
|
sb = (const initrd_superblock_t*) initrd;
|
2011-08-27 11:41:35 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( !String::StartsWith(sb->magic, "sortix-initrd") )
|
|
|
|
{
|
|
|
|
Panic("Invalid magic value in initrd. This means the ramdisk may have "
|
|
|
|
"been corrupted by the bootloader, or that an incompatible file "
|
|
|
|
"has been passed to the kernel.");
|
|
|
|
}
|
|
|
|
|
2012-09-22 16:53:11 -04:00
|
|
|
if ( strcmp(sb->magic, "sortix-initrd-1") == 0 )
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
|
|
|
Panic("Sortix initrd format version 1 is no longer supported.");
|
2011-08-27 11:41:35 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2012-09-22 16:53:11 -04:00
|
|
|
if ( strcmp(sb->magic, "sortix-initrd-2") != 0 )
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
|
|
|
Panic("The initrd has a format that isn't supported. Perhaps it is "
|
|
|
|
"too new? Try downgrade or regenerate the initrd.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( size < sb->fssize )
|
|
|
|
{
|
|
|
|
PanicF("The initrd said it is %u bytes, but the kernel was only passed "
|
|
|
|
"%zu bytes by the bootloader, which is not enough.", sb->fssize,
|
|
|
|
size);
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckSum();
|
2011-08-27 11:41:35 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
static bool ExtractDir(ioctx_t* ctx, uint32_t ino, Ref<Descriptor> node);
|
|
|
|
static bool ExtractFile(ioctx_t* ctx, uint32_t ino, Ref<Descriptor> file);
|
|
|
|
static bool ExtractNode(ioctx_t* ctx, uint32_t ino, Ref<Descriptor> node);
|
|
|
|
|
|
|
|
static bool ExtractDir(ioctx_t* ctx, uint32_t ino, Ref<Descriptor> dir)
|
|
|
|
{
|
|
|
|
size_t numfiles = GetNumFiles(ino);
|
|
|
|
for ( size_t i = 0; i < numfiles; i++ )
|
|
|
|
{
|
|
|
|
const char* name = GetFilename(ino, i);
|
|
|
|
if ( !name )
|
|
|
|
return false;
|
|
|
|
if ( IsDotOrDotDot(name) )
|
|
|
|
continue;
|
|
|
|
uint32_t childino = Traverse(ino, name);
|
|
|
|
if ( !childino )
|
|
|
|
return false;
|
|
|
|
const initrd_inode_t* child = GetInode(childino);
|
|
|
|
mode_t mode = InitRDModeToHost(child->mode);
|
|
|
|
if ( INITRD_S_ISDIR(child->mode) )
|
|
|
|
{
|
|
|
|
if ( dir->mkdir(ctx, name, mode) && errno != EEXIST )
|
|
|
|
return false;
|
2013-03-21 10:26:08 -04:00
|
|
|
Ref<Descriptor> desc = dir->open(ctx, name, O_SEARCH | O_DIRECTORY, 0);
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !desc )
|
|
|
|
return false;
|
|
|
|
if ( !ExtractNode(ctx, childino, desc) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( INITRD_S_ISREG(child->mode) )
|
|
|
|
{
|
2013-03-21 10:26:08 -04:00
|
|
|
Ref<Descriptor> desc = dir->open(ctx, name, O_WRITE | O_CREAT, mode);
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !desc )
|
|
|
|
return false;
|
|
|
|
if ( !ExtractNode(ctx, childino, desc) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ExtractFile(ioctx_t* ctx, uint32_t ino, Ref<Descriptor> file)
|
|
|
|
{
|
|
|
|
size_t filesize;
|
|
|
|
const uint8_t* data = Open(ino, &filesize);
|
|
|
|
if ( !data )
|
|
|
|
return false;
|
|
|
|
if ( file->truncate(ctx, filesize) != 0 )
|
|
|
|
return false;
|
|
|
|
size_t sofar = 0;
|
|
|
|
while ( sofar < filesize )
|
|
|
|
{
|
|
|
|
ssize_t numbytes = file->write(ctx, data + sofar, filesize - sofar);
|
|
|
|
if ( numbytes <= 0 )
|
|
|
|
return false;
|
|
|
|
sofar += numbytes;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ExtractNode(ioctx_t* ctx, uint32_t ino, Ref<Descriptor> node)
|
|
|
|
{
|
|
|
|
const initrd_inode_t* inode = GetInode(ino);
|
|
|
|
if ( !inode )
|
|
|
|
return false;
|
|
|
|
if ( node->chmod(ctx, InitRDModeToHost(inode->mode)) < 0 )
|
|
|
|
return false;
|
|
|
|
if ( node->chown(ctx, inode->uid, inode->gid) < 0 )
|
|
|
|
return false;
|
2013-03-22 15:19:45 -04:00
|
|
|
// TODO: utimens.
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( INITRD_S_ISDIR(inode->mode) )
|
|
|
|
if ( !ExtractDir(ctx, ino, node) )
|
|
|
|
return false;
|
|
|
|
if ( INITRD_S_ISREG(inode->mode) )
|
|
|
|
if ( !ExtractFile(ctx, ino, node) )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtractInto(Ref<Descriptor> desc)
|
|
|
|
{
|
|
|
|
ioctx_t ctx; SetupKernelIOCtx(&ctx);
|
|
|
|
return ExtractNode(&ctx, sb->root, desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExtractFromPhysicalInto(addr_t physaddr, size_t size, Ref<Descriptor> desc)
|
|
|
|
{
|
|
|
|
Init(physaddr, size);
|
|
|
|
return ExtractInto(desc);
|
|
|
|
}
|
|
|
|
|
2013-03-12 12:40:33 -04:00
|
|
|
void Delete()
|
|
|
|
{
|
|
|
|
size_t size = initrdsize;
|
|
|
|
|
|
|
|
initrd = NULL;
|
|
|
|
initrdsize = 0;
|
|
|
|
|
|
|
|
// Unmap the pages and return the physical frames for reallocation.
|
|
|
|
addr_t mapat = initrd_addr_alloc.from;
|
|
|
|
for ( size_t i = 0; i < size; i += Page::Size() )
|
|
|
|
{
|
|
|
|
addr_t addr = Memory::Unmap(mapat + i);
|
|
|
|
Page::Put(addr);
|
|
|
|
}
|
|
|
|
Memory::Flush();
|
|
|
|
|
|
|
|
// Free the used virtual address space.
|
|
|
|
FreeKernelAddress(&initrd_addr_alloc);
|
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
} // namespace InitRD
|
|
|
|
} // namespace Sortix
|