#include <CoreFoundation/CoreFoundation.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "bless.h"
#include "bless_private.h"
int BLSetBootBlocks(BLContextPtr context, const char * mountpoint,
const CFDataRef bootblocks) {
fbootstraptransfer_t bbr;
int fd;
int err;
if(bootblocks == NULL) {
contextprintf(context, kBLLogLevelError, "No boot blocks specified\n");
return 1;
}
if(CFDataGetLength(bootblocks) > kBootBlocksSize) {
contextprintf(context, kBLLogLevelError, "Boot blocks too large: %ld > %d\n",
CFDataGetLength(bootblocks), kBootBlocksSize);
return 1;
}
fd = open((char *)mountpoint, O_RDONLY);
if (fd == -1) {
contextprintf(context, kBLLogLevelError, "Can't open volume mount point for %s\n", mountpoint );
return 2;
}
bbr.fbt_offset = 0;
bbr.fbt_length = CFDataGetLength(bootblocks);
bbr.fbt_buffer = (char *)CFDataGetBytePtr(bootblocks);
err = fcntl(fd, F_WRITEBOOTSTRAP, &bbr);
if (err) {
contextprintf(context, kBLLogLevelError, "Can't write boot blocks\n" );
close(fd);
return 3;
} else {
contextprintf(context, kBLLogLevelVerbose, "Boot blocks written successfully\n" );
}
close(fd);
return 0;
}