2012-03-23 14:36:51 -04:00
|
|
|
/*******************************************************************************
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-27 08:50:52 -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 08:50:52 -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 08:50:52 -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 08:50:52 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
mkinitrd.cpp
|
|
|
|
Produces a simple ramdisk filesystem readable by the Sortix kernel.
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
*******************************************************************************/
|
2011-08-27 08:50:52 -04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
2012-03-23 14:36:51 -04:00
|
|
|
#include <fcntl.h>
|
2013-09-25 17:40:27 -04:00
|
|
|
#include <ioleast.h>
|
2012-03-23 14:36:51 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
2011-08-27 08:50:52 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sortix/initrd.h>
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
#include "crc32.h"
|
2013-01-29 14:01:46 -05:00
|
|
|
#include "rules.h"
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
uint32_t HostModeToInitRD(mode_t mode)
|
2011-08-27 08:50:52 -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;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
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;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct Node;
|
|
|
|
struct DirEntry;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct DirEntry
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
Node* node;
|
|
|
|
};
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct Node
|
|
|
|
{
|
|
|
|
char* path;
|
|
|
|
uint32_t ino;
|
|
|
|
uint32_t nlink;
|
|
|
|
size_t direntsused;
|
|
|
|
size_t direntslength;
|
|
|
|
DirEntry* dirents;
|
|
|
|
mode_t mode;
|
|
|
|
time_t ctime;
|
|
|
|
time_t mtime;
|
2012-12-09 07:23:18 -05:00
|
|
|
bool written;
|
|
|
|
size_t refcount;
|
2012-03-23 14:36:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void FreeNode(Node* node)
|
2011-12-01 15:44:11 -05:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( 1 < node->nlink ) { node->nlink--; return; }
|
|
|
|
for ( size_t i = 0; i < node->direntsused; i++ )
|
2011-12-01 15:44:11 -05:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
DirEntry* entry = node->dirents + i;
|
|
|
|
if ( strcmp(entry->name, ".") != 0 && strcmp(entry->name, "..") != 0 )
|
|
|
|
{
|
2012-12-09 07:23:18 -05:00
|
|
|
if ( --entry->node->refcount == 0 )
|
|
|
|
FreeNode(entry->node);
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
|
|
|
free(entry->name);
|
2011-12-01 15:44:11 -05:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
free(node->dirents);
|
|
|
|
free(node->path);
|
|
|
|
free(node);
|
2011-12-01 15:44:11 -05:00
|
|
|
}
|
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
struct CacheEntry
|
|
|
|
{
|
|
|
|
ino_t ino;
|
|
|
|
dev_t dev;
|
|
|
|
Node* node;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t cacheused = 0;
|
|
|
|
size_t cachelen = 0;
|
|
|
|
CacheEntry* cache = NULL;
|
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
InclusionRules path_filter;
|
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
Node* LookupCache(dev_t dev, ino_t ino)
|
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < cacheused; i++ )
|
|
|
|
if ( cache[i].dev == dev && cache[i].ino == ino )
|
|
|
|
return cache[i].node;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddToCache(Node* node, dev_t dev, ino_t ino)
|
|
|
|
{
|
|
|
|
if ( cacheused == cachelen )
|
|
|
|
{
|
|
|
|
size_t newcachelen = cachelen ? 2 * cachelen : 256;
|
|
|
|
size_t newcachesize = newcachelen * sizeof(CacheEntry);
|
|
|
|
CacheEntry* newcache = (CacheEntry*) realloc(cache, newcachesize);
|
|
|
|
if ( !newcache )
|
|
|
|
return false;
|
|
|
|
cache = newcache;
|
|
|
|
cachelen = newcachelen;
|
|
|
|
}
|
|
|
|
size_t index = cacheused++;
|
|
|
|
cache[index].ino = ino;
|
|
|
|
cache[index].dev = dev;
|
|
|
|
cache[index].node = node;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
Node* RecursiveSearch(const char* real_path, const char* virt_path,
|
|
|
|
uint32_t* ino, Node* parent = NULL)
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2013-01-29 14:01:46 -05:00
|
|
|
printf("%s\n", virt_path);
|
|
|
|
|
|
|
|
if ( virt_path[0] == '/' && !virt_path[1] )
|
|
|
|
virt_path = "";
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct stat st;
|
2013-01-29 14:01:46 -05:00
|
|
|
if ( lstat(real_path, &st) ) { perror(real_path); return NULL; }
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
Node* cached = LookupCache(st.st_dev, st.st_ino);
|
|
|
|
if ( cached ) { cached->refcount++; return cached; }
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
Node* node = (Node*) calloc(1, sizeof(Node));
|
|
|
|
if ( !node ) { return NULL; }
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
node->refcount = 1;
|
2012-03-23 14:36:51 -04:00
|
|
|
node->mode = st.st_mode;
|
|
|
|
node->ino = (*ino)++;
|
2013-03-22 15:19:45 -04:00
|
|
|
node->ctime = st.st_ctim.tv_sec;
|
|
|
|
node->mtime = st.st_mtim.tv_sec;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
char* real_path_clone = strdup(real_path);
|
|
|
|
if ( !real_path_clone ) { perror("strdup"); free(node); return NULL; }
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
node->path = real_path_clone;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
if ( !S_ISDIR(st.st_mode) )
|
|
|
|
{
|
|
|
|
if ( !AddToCache(node, st.st_dev, st.st_ino) )
|
|
|
|
{
|
2013-01-29 14:01:46 -05:00
|
|
|
free(real_path_clone);
|
2012-12-09 07:23:18 -05:00
|
|
|
free(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
DIR* dir = opendir(real_path);
|
|
|
|
if ( !dir ) { perror(real_path); FreeNode(node); return NULL; }
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
size_t real_path_len = strlen(real_path);
|
|
|
|
size_t virt_path_len = strlen(virt_path);
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
bool successful = true;
|
|
|
|
struct dirent* entry;
|
|
|
|
while ( (entry = readdir(dir)) )
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
size_t namelen = strlen(entry->d_name);
|
2013-01-29 14:01:46 -05:00
|
|
|
|
|
|
|
size_t virt_subpath_len = virt_path_len + 1 + namelen;
|
|
|
|
char* virt_subpath = (char*) malloc(virt_subpath_len+1);
|
|
|
|
if ( !virt_subpath ) { perror("malloc"); successful = false; break; }
|
|
|
|
stpcpy(stpcpy(stpcpy(virt_subpath, virt_path), "/"), entry->d_name);
|
|
|
|
|
|
|
|
if ( strcmp(entry->d_name, ".") != 0 &&
|
|
|
|
strcmp(entry->d_name, "..") != 0 &&
|
|
|
|
!path_filter.IncludesPath(virt_subpath) )
|
|
|
|
{
|
|
|
|
free(virt_subpath);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t real_subpath_len = real_path_len + 1 + namelen;
|
|
|
|
char* real_subpath = (char*) malloc(real_subpath_len+1);
|
|
|
|
if ( !real_subpath ) { free(virt_subpath); perror("malloc"); successful = false; break; }
|
|
|
|
stpcpy(stpcpy(stpcpy(real_subpath, real_path), "/"), entry->d_name);
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
Node* child = NULL;
|
2013-01-29 14:01:46 -05:00
|
|
|
if ( !strcmp(entry->d_name, ".") )
|
|
|
|
child = node;
|
|
|
|
if ( !strcmp(entry->d_name, "..") )
|
|
|
|
child = parent ? parent : node;
|
|
|
|
if ( !child )
|
|
|
|
child = RecursiveSearch(real_subpath, virt_subpath, ino, node);
|
|
|
|
free(real_subpath);
|
|
|
|
free(virt_subpath);
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( !child ) { successful = false; break; }
|
|
|
|
|
|
|
|
if ( node->direntsused == node->direntslength )
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
size_t oldlength = node->direntslength;
|
|
|
|
size_t newlength = oldlength ? 2 * oldlength : 8;
|
|
|
|
size_t newsize = sizeof(DirEntry) * newlength;
|
|
|
|
DirEntry* newdirents = (DirEntry*) realloc(node->dirents, newsize);
|
|
|
|
if ( !newdirents ) { perror("realloc"); successful = false; break; }
|
|
|
|
node->dirents = newdirents;
|
|
|
|
node->direntslength = newlength;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
char* nameclone = strdup(entry->d_name);
|
|
|
|
if ( !nameclone ) { perror("strdup"); successful = false; break; }
|
|
|
|
|
|
|
|
DirEntry* entry = node->dirents + node->direntsused++;
|
|
|
|
|
|
|
|
entry->name = nameclone;
|
|
|
|
entry->node = child;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
closedir(dir);
|
2012-12-09 07:23:18 -05:00
|
|
|
if ( !successful || !AddToCache(node, st.st_dev, st.st_ino) )
|
|
|
|
{
|
|
|
|
FreeNode(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteNode(struct initrd_superblock* sb, int fd, const char* outputname,
|
|
|
|
Node* node)
|
|
|
|
{ {
|
2012-12-09 07:23:18 -05:00
|
|
|
if ( node->written )
|
|
|
|
return true;
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t filesize = 0;
|
|
|
|
uint32_t origfssize = sb->fssize;
|
|
|
|
uint32_t dataoff = origfssize;
|
|
|
|
uint32_t filestart = dataoff;
|
|
|
|
|
|
|
|
if ( S_ISLNK(node->mode) ) // Symbolic link
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
const size_t NAME_SIZE = 1024UL;
|
|
|
|
char name[NAME_SIZE];
|
|
|
|
ssize_t namelen = readlink(node->path, name, NAME_SIZE);
|
|
|
|
if ( namelen < 0 ) { goto ioreadlink; }
|
|
|
|
filesize = (uint32_t) namelen;
|
|
|
|
if ( pwriteall(fd, name, filesize, dataoff) < filesize ) goto ioread;
|
|
|
|
dataoff += filesize;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
else if ( S_ISREG(node->mode) ) // Regular file
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
int nodefd = open(node->path, O_RDONLY);
|
|
|
|
if ( nodefd < 0 ) { goto ioopen; }
|
|
|
|
const size_t BUFFER_SIZE = 16UL * 1024UL;
|
|
|
|
uint8_t buffer[BUFFER_SIZE];
|
|
|
|
ssize_t amount;
|
|
|
|
while ( 0 < (amount = read(nodefd, buffer, BUFFER_SIZE)) )
|
|
|
|
{
|
|
|
|
if ( pwriteall(fd, buffer, amount, dataoff) < (size_t) amount )
|
|
|
|
{
|
|
|
|
close(nodefd);
|
|
|
|
goto iowrite;
|
|
|
|
}
|
|
|
|
dataoff += amount;
|
|
|
|
filesize += amount;
|
|
|
|
}
|
|
|
|
close(nodefd);
|
|
|
|
if ( amount < 0 ) { goto ioread; }
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
else if ( S_ISDIR(node->mode) ) // Directory
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
for ( size_t i = 0; i < node->direntsused; i++ )
|
|
|
|
{
|
|
|
|
DirEntry* entry = node->dirents + i;
|
|
|
|
const char* name = entry->name;
|
|
|
|
size_t namelen = strlen(entry->name);
|
|
|
|
struct initrd_dirent dirent;
|
|
|
|
dirent.inode = entry->node->ino;
|
|
|
|
dirent.namelen = (uint16_t) namelen;
|
|
|
|
dirent.reclen = sizeof(dirent) + dirent.namelen + 1;
|
2012-12-09 07:23:18 -05:00
|
|
|
dirent.reclen = (dirent.reclen+3)/4*4; // Align entries.
|
2012-03-23 14:36:51 -04:00
|
|
|
size_t entsize = sizeof(dirent);
|
|
|
|
ssize_t hdramt = pwriteall(fd, &dirent, entsize, dataoff);
|
|
|
|
ssize_t nameamt = pwriteall(fd, name, namelen+1, dataoff + entsize);
|
|
|
|
if ( hdramt < (ssize_t) entsize || nameamt < (ssize_t) (namelen+1) )
|
|
|
|
goto iowrite;
|
2012-12-09 07:23:18 -05:00
|
|
|
size_t padding = dirent.reclen - (entsize + (namelen+1));
|
|
|
|
for ( size_t i = 0; i < padding; i++ )
|
|
|
|
{
|
|
|
|
uint8_t nul = 0;
|
|
|
|
if ( pwrite(fd, &nul, 1, dataoff+entsize+namelen+1+i) != 1 )
|
|
|
|
goto iowrite;
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
filesize += dirent.reclen;
|
|
|
|
dataoff += dirent.reclen;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct initrd_inode inode;
|
|
|
|
inode.mode = HostModeToInitRD(node->mode);
|
|
|
|
inode.uid = 1;
|
|
|
|
inode.gid = 1;
|
|
|
|
inode.nlink = node->nlink;
|
|
|
|
inode.ctime = (uint64_t) node->ctime;
|
|
|
|
inode.mtime = (uint64_t) node->mtime;
|
|
|
|
inode.dataoffset = filestart;
|
|
|
|
inode.size = filesize;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t inodepos = sb->inodeoffset + node->ino * sb->inodesize;
|
|
|
|
uint32_t inodesize = sizeof(inode);
|
|
|
|
if ( pwriteall(fd, &inode, inodesize, inodepos) < inodesize ) goto iowrite;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t increment = dataoff - origfssize;
|
|
|
|
sb->fssize += increment;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
return node->written = true;
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
|
|
|
ioreadlink:
|
|
|
|
error(0, errno, "readlink: %s", node->path);
|
|
|
|
return false;
|
|
|
|
ioopen:
|
|
|
|
error(0, errno, "open: %s", node->path);
|
|
|
|
return false;
|
|
|
|
ioread:
|
|
|
|
error(0, errno, "read: %s", node->path);
|
|
|
|
return false;
|
|
|
|
iowrite:
|
|
|
|
error(0, errno, "write: %s", outputname);
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
bool WriteNodeRecursive(struct initrd_superblock* sb, int fd,
|
|
|
|
const char* outputname, Node* node)
|
|
|
|
{
|
|
|
|
if ( !WriteNode(sb, fd, outputname, node) ) { return false; }
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( !S_ISDIR(node->mode) ) { return true; }
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
for ( size_t i = 0; i < node->direntsused; i++ )
|
|
|
|
{
|
|
|
|
DirEntry* entry = node->dirents + i;
|
|
|
|
const char* name = entry->name;
|
|
|
|
Node* child = entry->node;
|
|
|
|
if ( !strcmp(name, ".") || !strcmp(name, ".." ) ) { continue; }
|
|
|
|
if ( !WriteNodeRecursive(sb, fd, outputname, child) ) { return false; }
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
bool Format(const char* outputname, int fd, uint32_t inodecount, Node* root)
|
|
|
|
{
|
|
|
|
struct initrd_superblock sb;
|
|
|
|
memset(&sb, 0, sizeof(sb));
|
|
|
|
strncpy(sb.magic, "sortix-initrd-2", sizeof(sb.magic));
|
|
|
|
sb.revision = 0;
|
|
|
|
sb.fssize = sizeof(sb);
|
|
|
|
sb.inodesize = sizeof(initrd_inode);
|
|
|
|
sb.inodeoffset = sizeof(sb);
|
|
|
|
sb.inodecount = inodecount;
|
|
|
|
sb.root = root->ino;
|
|
|
|
|
|
|
|
uint32_t inodebytecount = sb.inodesize * sb.inodecount;
|
|
|
|
sb.fssize += inodebytecount;
|
|
|
|
|
|
|
|
if ( !WriteNodeRecursive(&sb, fd, outputname, root) ) { return false; }
|
|
|
|
|
|
|
|
uint32_t crcsize = sizeof(uint32_t);
|
|
|
|
sb.sumalgorithm = INITRD_ALGO_CRC32;
|
|
|
|
sb.sumsize = crcsize;
|
|
|
|
sb.fssize += sb.sumsize;
|
|
|
|
|
|
|
|
if ( pwriteall(fd, &sb, sizeof(sb), 0) < sizeof(sb) )
|
|
|
|
{
|
|
|
|
error(0, errno, "write: %s", outputname);
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t checksize = sb.fssize - sb.sumsize;
|
|
|
|
uint32_t crc;
|
|
|
|
if ( !CRC32File(&crc, outputname, fd, 0, checksize) ) { return false; }
|
|
|
|
if ( pwriteall(fd, &crc, crcsize, checksize) < crcsize ) { return false; }
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
bool Format(const char* pathname, uint32_t inodecount, Node* root)
|
|
|
|
{
|
|
|
|
int fd = open(pathname, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
|
|
|
bool result = Format(pathname, fd, inodecount, root);
|
|
|
|
close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
void Usage(FILE* fp, const char* argv0)
|
|
|
|
{
|
2013-01-29 14:01:46 -05:00
|
|
|
fprintf(fp, "Usage: %s <ROOT> -o <DEST> [-f <PATH-FILTER>]\n", argv0);
|
2012-03-23 14:36:51 -04:00
|
|
|
fprintf(fp, "Creates a init ramdisk for the Sortix kernel.\n");
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
void Help(FILE* fp, const char* argv0)
|
|
|
|
{
|
|
|
|
Usage(fp, argv0);
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
void Version(FILE* fp, const char* argv0)
|
|
|
|
{
|
2013-06-26 18:15:53 -04:00
|
|
|
(void) argv0;
|
2013-01-29 14:01:46 -05:00
|
|
|
fprintf(fp, "mkinitrd 0.4\n");
|
|
|
|
fprintf(fp, "Copyright (C) 2012, 2013 Jonas 'Sortie' Termansen\n");
|
2012-03-23 14:36:51 -04:00
|
|
|
fprintf(fp, "This is free software; see the source for copying conditions. There is NO\n");
|
|
|
|
fprintf(fp, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
|
|
|
|
fprintf(fp, "website: http://www.maxsi.org/software/sortix/\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
const char* argv0 = argv[0];
|
|
|
|
if ( argc < 2 ) { Usage(stdout, argv0); exit(0); }
|
|
|
|
const char* dest = NULL;
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
int argsleft = argc - i - 1;
|
|
|
|
const char* arg = argv[i];
|
|
|
|
if ( arg[0] != '-' ) { continue; }
|
|
|
|
argv[i] = NULL;
|
|
|
|
if ( !strcmp(arg, "--") ) { break; }
|
|
|
|
if ( !strcmp(arg, "--help") ) { Help(stdout, argv0); exit(0); }
|
|
|
|
if ( !strcmp(arg, "--usage") ) { Usage(stdout, argv0); exit(0); }
|
|
|
|
if ( !strcmp(arg, "--version") ) { Version(stdout, argv0); exit(0); }
|
|
|
|
if ( !strcmp(arg, "-o") || !strcmp(arg, "--output") )
|
|
|
|
{
|
|
|
|
if ( argsleft < 1 )
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
fprintf(stderr, "No output file specified\n");
|
|
|
|
Usage(stderr, argv0);
|
|
|
|
exit(1);
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
dest = argv[++i]; argv[i] = NULL;
|
|
|
|
continue;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2013-01-29 14:01:46 -05:00
|
|
|
if ( !strcmp(arg, "-f") || !strcmp(arg, "--filter") )
|
|
|
|
{
|
|
|
|
if ( argsleft < 1 )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "No filter rule file specified\n");
|
|
|
|
Usage(stderr, argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
const char* path = argv[++i]; argv[i] = NULL;
|
|
|
|
FILE* fp = fopen(path, "r");
|
|
|
|
if ( !fp )
|
|
|
|
error(1, errno, "%s", path);
|
|
|
|
if ( !path_filter.AddRulesFromFile(fp, stderr, path) )
|
|
|
|
exit(1);
|
|
|
|
fclose(fp);
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
|
|
|
Usage(stderr, argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
const char* rootstr = NULL;
|
|
|
|
int args = 0;
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
if ( !argv[i] ) { continue; }
|
|
|
|
args++;
|
|
|
|
rootstr = argv[i];
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
const char* errmsg = NULL;
|
|
|
|
if ( !errmsg && args < 1 ) { errmsg = "no root specified"; }
|
|
|
|
if ( !errmsg && 1 < args ) { errmsg = "too many roots"; }
|
|
|
|
if ( !errmsg && !dest ) { errmsg = "no destination specified"; }
|
|
|
|
|
|
|
|
if ( errmsg )
|
2011-12-01 15:44:11 -05:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
fprintf(stderr, "%s: %s\n", argv0, errmsg),
|
|
|
|
Usage(stderr, argv0);
|
|
|
|
exit(1);
|
2011-12-01 15:44:11 -05:00
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t inodecount = 1;
|
2013-01-29 14:01:46 -05:00
|
|
|
Node* root = RecursiveSearch(rootstr, "/", &inodecount);
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( !root ) { exit(1); }
|
|
|
|
|
|
|
|
if ( !Format(dest, inodecount, root) ) { exit(1); }
|
|
|
|
|
|
|
|
FreeNode(root);
|
|
|
|
|
2011-08-27 08:50:52 -04:00
|
|
|
return 0;
|
|
|
|
}
|