2004-12-09 22:57:09 +00:00
|
|
|
static __attribute__((used)) char* rcsid = "$CVSHeader$";
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) 2004 Laurent Vivier <LaurentVivier@wanadoo.fr>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libemile.h"
|
|
|
|
#include "emile.h"
|
|
|
|
#include "bootblock.h"
|
|
|
|
|
|
|
|
static int copy_file(int fd, char* file)
|
|
|
|
{
|
|
|
|
int source;
|
|
|
|
int size_read;
|
|
|
|
int size_written;
|
|
|
|
int total;
|
|
|
|
static char buffer[FLOPPY_SECTOR_SIZE];
|
|
|
|
|
|
|
|
source = open(file, O_RDONLY);
|
|
|
|
if (source < 0)
|
|
|
|
{
|
|
|
|
close(source);
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_CANNOT_OPEN_FILE;
|
2004-12-09 22:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
size_read = read(source, buffer, FLOPPY_SECTOR_SIZE);
|
|
|
|
if (size_read == FLOPPY_SECTOR_SIZE)
|
|
|
|
{
|
|
|
|
size_written = write(fd, buffer, FLOPPY_SECTOR_SIZE);
|
|
|
|
total += size_written;
|
|
|
|
if (size_written != FLOPPY_SECTOR_SIZE)
|
|
|
|
{
|
|
|
|
close(source);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (size_read == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
memset(buffer + size_read, 0, FLOPPY_SECTOR_SIZE - size_read);
|
|
|
|
size_written = write(fd, buffer, FLOPPY_SECTOR_SIZE);
|
|
|
|
total += size_written;
|
|
|
|
if (size_written != FLOPPY_SECTOR_SIZE)
|
|
|
|
{
|
|
|
|
close(source);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(source);
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pad_image(int fd, int size)
|
|
|
|
{
|
|
|
|
static char buffer[FLOPPY_SECTOR_SIZE];
|
|
|
|
int size_written;
|
|
|
|
int total;
|
|
|
|
|
|
|
|
if (size % FLOPPY_SECTOR_SIZE) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: pad size is not a multiple of sector size\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buffer, 0, FLOPPY_SECTOR_SIZE);
|
|
|
|
total = 0;
|
|
|
|
while (size > 0) {
|
|
|
|
size_written = write(fd, buffer, FLOPPY_SECTOR_SIZE);
|
|
|
|
total += size_written;
|
|
|
|
if (size_written != FLOPPY_SECTOR_SIZE) {
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
size -= size_written;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aggregate(int fd, char* first_level, char* second_level, char* kernel_image, char* ramdisk)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int total;
|
|
|
|
|
|
|
|
ret = copy_file(fd, first_level);
|
|
|
|
if (ret < 0)
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_CANNOT_WRITE_FIRST;
|
2004-12-09 22:57:09 +00:00
|
|
|
total = ret;
|
|
|
|
|
|
|
|
ret = copy_file(fd, second_level);
|
|
|
|
if (ret < 0)
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_CANNOT_WRITE_SECOND;
|
2004-12-09 22:57:09 +00:00
|
|
|
total += ret;
|
|
|
|
|
|
|
|
if (kernel_image != NULL)
|
|
|
|
{
|
|
|
|
ret = copy_file(fd, kernel_image);
|
|
|
|
if (ret < 0)
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_CANNOT_WRITE_KERNEL;
|
2004-12-09 22:57:09 +00:00
|
|
|
total += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ramdisk != NULL)
|
|
|
|
{
|
|
|
|
ret = copy_file(fd, ramdisk);
|
|
|
|
if (ret < 0)
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_CANNOT_WRITE_RAMDISK;
|
2004-12-09 22:57:09 +00:00
|
|
|
total += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pad_image(fd, 1474560 - total);
|
|
|
|
if (ret < 0)
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_CANNOT_WRITE_PAD;
|
2004-12-09 22:57:09 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-30 15:33:14 +00:00
|
|
|
static int is_url(char *path)
|
|
|
|
{
|
|
|
|
return path && ((strncmp(path, "iso9660:", strlen("iso9660:")) == 0) ||
|
|
|
|
(strncmp(path, "container:", strlen("container:")) == 0) ||
|
|
|
|
(strncmp(path, "block:", strlen("block:")) == 0) ||
|
|
|
|
(strncmp(path, "ext2:", strlen("ext2")) == 0));
|
|
|
|
}
|
2004-12-09 22:57:09 +00:00
|
|
|
int emile_floppy_create_image(char* first_level, char* second_level,
|
|
|
|
char* kernel_image, char* ramdisk,
|
2005-11-27 23:45:18 +00:00
|
|
|
char* image)
|
2004-12-09 22:57:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int fd;
|
2005-11-30 15:33:14 +00:00
|
|
|
char *kernel_url = NULL;
|
|
|
|
char *ramdisk_url = NULL;
|
2005-11-27 23:45:18 +00:00
|
|
|
char configuration[1024];
|
2004-12-09 22:57:09 +00:00
|
|
|
|
|
|
|
if (image == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fd = open(image, O_RDWR|O_CREAT|O_TRUNC,
|
|
|
|
S_IRUSR| S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
|
|
|
|
if (fd == -1)
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_CANNOT_CREATE_IMAGE;
|
2004-12-09 22:57:09 +00:00
|
|
|
|
|
|
|
/* aggregating files: first, second, kernel, ramdisk */
|
|
|
|
|
|
|
|
if (first_level == NULL)
|
|
|
|
{
|
|
|
|
close(fd);
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_MISSING_FIRST;
|
2004-12-09 22:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (second_level == NULL)
|
|
|
|
{
|
|
|
|
close(fd);
|
2004-12-12 22:59:38 +00:00
|
|
|
return EEMILE_MISSING_SECOND;
|
2004-12-09 22:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (kernel_image == NULL)
|
|
|
|
fprintf(stderr, "WARNING: kernel image file not defined\n");
|
|
|
|
|
2005-11-30 15:33:14 +00:00
|
|
|
if ( is_url(kernel_image) )
|
|
|
|
{
|
|
|
|
kernel_url = kernel_image;
|
|
|
|
kernel_image = NULL;
|
|
|
|
}
|
|
|
|
if ( is_url(ramdisk) )
|
|
|
|
{
|
|
|
|
ramdisk_url = ramdisk;
|
|
|
|
ramdisk = NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-09 22:57:09 +00:00
|
|
|
ret = aggregate(fd, first_level, second_level, kernel_image, ramdisk);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
2004-12-12 22:59:38 +00:00
|
|
|
return ret;
|
2004-12-09 22:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set first level info */
|
|
|
|
|
2004-12-12 22:59:38 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2004-12-09 22:57:09 +00:00
|
|
|
ret = emile_first_set_param(fd, EMILE_FIRST_TUNE_DRIVE |
|
|
|
|
EMILE_FIRST_TUNE_OFFSET|
|
|
|
|
EMILE_FIRST_TUNE_SIZE,
|
2004-12-10 00:26:10 +00:00
|
|
|
1, FIRST_LEVEL_SIZE,
|
2004-12-24 21:48:01 +00:00
|
|
|
emile_file_get_size(second_level));
|
2004-12-09 22:57:09 +00:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
2004-12-12 22:59:38 +00:00
|
|
|
return ret;
|
2004-12-09 22:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set second level info */
|
|
|
|
|
2005-11-30 15:33:14 +00:00
|
|
|
*configuration = 0;
|
2005-11-27 23:50:36 +00:00
|
|
|
if (kernel_image)
|
|
|
|
{
|
|
|
|
sprintf(configuration, "kernel block:(fd0)0x%lx\n",
|
2005-11-27 23:45:18 +00:00
|
|
|
FIRST_LEVEL_SIZE + emile_file_get_size(second_level));
|
2005-11-27 23:50:36 +00:00
|
|
|
}
|
2005-11-30 15:33:14 +00:00
|
|
|
else if (kernel_url)
|
|
|
|
sprintf(configuration, "kernel %s\n", kernel_url);
|
|
|
|
|
|
|
|
if (ramdisk)
|
|
|
|
sprintf(configuration + strlen(configuration),
|
|
|
|
"initrd block:(fd0)0x%lx,0x%lx\n", FIRST_LEVEL_SIZE +
|
|
|
|
emile_file_get_size(second_level) +
|
|
|
|
emile_file_get_size(kernel_image),
|
|
|
|
emile_file_get_size(ramdisk));
|
|
|
|
else if (ramdisk_url)
|
|
|
|
sprintf(configuration + strlen(configuration),
|
|
|
|
"initrd %s\n", ramdisk_url);
|
|
|
|
|
|
|
|
sprintf(configuration + strlen(configuration), "vga default");
|
|
|
|
ret = emile_second_set_configuration(fd, configuration);
|
2004-12-09 22:57:09 +00:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2004-12-12 22:59:38 +00:00
|
|
|
return ret;
|
2004-12-09 22:57:09 +00:00
|
|
|
}
|