#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/sysmacros.h>
#include <sys/multiboot.h>
#include "bblk_einfo.h"
#include "boot_utils.h"
#include "mboot_extra.h"
void
add_version(const char *ifile, const char *ofile, char *version)
{
int fd;
int ret;
uint32_t buf_size;
uint32_t mboot_off;
uint32_t extra;
uint32_t avail_space;
multiboot_header_t *mboot;
struct stat sb;
char *buf;
bblk_hs_t hs;
fd = open(ifile, O_RDONLY);
if (fd == -1) {
perror("open");
return;
}
if (fstat(fd, &sb) == -1) {
perror("fstat");
close(fd);
return;
}
buf_size = P2ROUNDUP(sb.st_size + SECTOR_SIZE, SECTOR_SIZE);
buf = malloc(buf_size);
if (buf == NULL) {
perror("malloc");
close(fd);
return;
}
ret = read(fd, buf, sb.st_size);
if (ret != sb.st_size) {
perror("read");
free(buf);
close(fd);
return;
}
close(fd);
if (find_multiboot(buf, MBOOT_SCAN_SIZE, &mboot_off)
!= BC_SUCCESS) {
printf("Unable to find multiboot header\n");
free(buf);
return;
}
mboot = (multiboot_header_t *)(buf + mboot_off);
mboot->load_addr = 0;
mboot->load_end_addr = sb.st_size;
hs.src_buf = (unsigned char *)buf;
hs.src_size = sb.st_size;
extra = P2ROUNDUP(sb.st_size, 8);
avail_space = buf_size - extra;
memset(buf+sb.st_size, 0, buf_size - sb.st_size);
add_einfo(buf + extra, version, &hs, avail_space);
fd = open(ofile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1) {
perror("open");
free(buf);
return;
}
ret = write(fd, buf, buf_size);
close(fd);
free(buf);
}