You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libisoburn/xorriso/write_run.c

2887 lines
100 KiB

/* xorriso - creates, loads, manipulates and burns ISO 9660 filesystem images.
Copyright 2007-2014 Thomas Schmitt, <scdbackup@gmx.net>
Provided under GPL version 2 or later.
This file contains functions which are needed to write sessions.
*/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#else
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#endif
#ifdef Xorriso_standalonE
#ifdef Xorriso_with_libjtE
#include "../libjte/libjte.h"
#endif
#else
#ifdef Xorriso_with_libjtE
#include <libjte/libjte.h>
#endif
#endif /* ! Xorriso_standalonE */
#include "xorriso.h"
#include "xorriso_private.h"
#include "lib_mgt.h"
#include "drive_mgt.h"
#include "iso_img.h"
#include "iso_tree.h"
#include "write_run.h"
/* @param flag bit0= talk of -as cdrecord -multi rather than of -close
*/
int Xorriso_check_multi(struct XorrisO *xorriso, struct burn_drive *drive,
int flag)
{
int profile_no= 0, ret;
struct burn_multi_caps *caps= NULL;
char profile_name[80];
if(xorriso->auto_close)
xorriso->do_close= 0;
if(!xorriso->do_close) {
burn_disc_get_profile(drive, &profile_no, profile_name);
if(profile_no == 0x14) { /* DVD-RW sequential */
ret= burn_disc_get_multi_caps(drive, BURN_WRITE_TAO, &caps, 0);
if(caps != NULL)
burn_disc_free_multi_caps(&caps);
if(ret == 0) {
if(xorriso->auto_close) {
sprintf(xorriso->info_text,
"-close \"as_needed\" triggered -close \"on\"");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
xorriso->do_close= 1;
} else if(flag & 1) {
sprintf(xorriso->info_text,
"This DVD-RW media can only be written without option -multi");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
sprintf(xorriso->info_text,
"Possibly it was blanked by blank=deformat_quickest");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "HINT", 0);
sprintf(xorriso->info_text,
"After writing a session without -multi, apply blank=all");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "HINT", 0);
return(0);
} else {
sprintf(xorriso->info_text,
"This DVD-RW media can only be written with -close \"on\"");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
sprintf(xorriso->info_text,
"Possibly it was blanked by -blank \"deformat_quickest\"");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "HINT", 0);
sprintf(xorriso->info_text,
"After writing a session with -close \"on\", apply -blank \"all\"");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "HINT", 0);
return(0);
}
}
} else if(profile_no == 0x15) { /* DVD-RW DL */
if(xorriso->auto_close) {
sprintf(xorriso->info_text,
"-close \"as_needed\" triggered -close \"on\"");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
xorriso->do_close= 1;
} else if(flag & 1) {
sprintf(xorriso->info_text,
"DVD-R DL media can only be written without option -multi");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
return(0);
} else {
sprintf(xorriso->info_text,
"DVD-R DL media can only be written with -close \"on\"");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
return(0);
}
}
}
return(1);
}
int Xorriso_make_write_options(
struct XorrisO *xorriso, struct burn_drive *drive,
struct burn_write_opts **burn_options, int flag)
{
int drive_role, stream_mode= 0, ret, profile;
char profile_name[80];
enum burn_disc_status s;
*burn_options= burn_write_opts_new(drive);
if(*burn_options==NULL) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"Cannot allocate option set");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
return(0);
}
burn_write_opts_set_simulate(*burn_options, !!xorriso->do_dummy);
drive_role= burn_drive_get_drive_role(drive);
burn_write_opts_set_multi(*burn_options,
!(xorriso->do_close || drive_role==0 || drive_role==3));
ret= burn_disc_get_profile(drive, &profile, profile_name);
if(ret > 0) {
s= isoburn_disc_get_status(drive);
if(xorriso->auto_close && xorriso->do_close == 0 &&
profile == 0x14 && s == BURN_DISC_BLANK)
/* Prepare for missing feature 21h despite drive's announcement */
burn_write_opts_set_fail21h_sev(*burn_options, "NOTE");
}
if(xorriso->write_speed != -2)
burn_drive_set_speed(drive, 0, xorriso->write_speed);
if(xorriso->do_stream_recording == 1)
stream_mode= 1;
else if(xorriso->do_stream_recording == 2)
stream_mode= 51200; /* 100 MB */
else if(xorriso->do_stream_recording >= 16)
stream_mode= xorriso->do_stream_recording;
burn_write_opts_set_stream_recording(*burn_options, stream_mode);
#ifdef Xorriso_dvd_obs_default_64K
if(xorriso->dvd_obs == 0)
burn_write_opts_set_dvd_obs(*burn_options, 64 * 1024);
else
#endif
burn_write_opts_set_dvd_obs(*burn_options, xorriso->dvd_obs);
burn_write_opts_set_stdio_fsync(*burn_options, xorriso->stdio_sync);
burn_write_opts_set_underrun_proof(*burn_options, 1);
return(1);
}
/* @param flag bit0= do not write but only prepare and return size in sectors
bit1= do not use isoburn wrappers, do not assume libisofs
*/
int Xorriso_sanitize_image_size(struct XorrisO *xorriso,
struct burn_drive *drive, struct burn_disc *disc,
struct burn_write_opts *burn_options, int flag)
{
int ret, img_sectors, num_sessions= 0, num_tracks= 0, padding= 0, profile;
int media_space, lba, nwa, multi_emul_blocks= 0;
char profile_name[80];
struct burn_session **sessions;
struct burn_track **tracks;
enum burn_disc_status s;
img_sectors= burn_disc_get_sectors(disc);
sessions= burn_disc_get_sessions(disc, &num_sessions);
if(sessions==NULL || num_sessions < 1) {
no_track:;
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"Program error : no track in prepared disc");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
{ret= -1; goto ex;}
}
tracks= burn_session_get_tracks(sessions[0], &num_tracks);
if(tracks==NULL || num_tracks < 1)
goto no_track;
padding= 0;
ret= burn_disc_get_profile(drive, &profile, profile_name);
padding= xorriso->padding / 2048;
if(xorriso->padding > padding * 2048)
padding++;
if(img_sectors>0 && ret>0 &&
(profile==0x09 || profile==0x0a)) { /* CD-R , CD-RW */
if(img_sectors + padding < Xorriso_cd_min_track_sizE) {
padding= Xorriso_cd_min_track_sizE - img_sectors;
sprintf(xorriso->info_text,
"Expanded track to minimum size of %d sectors",
Xorriso_cd_min_track_sizE);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
}
}
if(xorriso->alignment == 0 && ! (xorriso->no_emul_toc & 1)) {
ret= isoburn_needs_emulation(drive);
if(ret > 0) {
/* Take care that the session is padded up to the future NWA.
Else with padding < 32 it could happen that PVDs from older
sessions survive and confuse -rom_toc_scan.
*/
xorriso->alignment= 32;
s= isoburn_disc_get_status(drive);
if(s == BURN_DISC_BLANK) {
/* Count blocks before nwa as part of the image */;
ret= isoburn_disc_track_lba_nwa(drive, burn_options, 0, &lba, &nwa);
if(ret <= 0)
nwa= 0;
multi_emul_blocks= nwa;
}
}
}
if(!(flag & 2)) {
#ifdef Xorriso_with_libjtE
/* JTE : no multi-session, no_emul_toc, padding in libisofs */
if(xorriso->libjte_handle != NULL)
padding= 0;
#endif /* ! Xorriso_with_libjtE */
if(xorriso->do_padding_by_libisofs)
padding= 0;
}
if(xorriso->alignment > 0) {
if(img_sectors > 0) {
ret= isoburn_disc_track_lba_nwa(drive, burn_options, 0, &lba, &nwa);
if(ret <= 0)
nwa= 0;
lba= (nwa + img_sectors + padding) % xorriso->alignment;
if(lba > 0)
padding+= xorriso->alignment - lba;
}
}
burn_track_define_data(tracks[0], 0, padding * 2048, 0, BURN_MODE1);
Xorriso_process_msg_queues(xorriso,0);
if(flag&2)
media_space= burn_disc_available_space(drive, burn_options) /
(off_t) 2048;
else
media_space= isoburn_disc_available_space(drive, burn_options) /
(off_t) 2048;
if(media_space < img_sectors + padding) {
Xorriso_process_msg_queues(xorriso,0);
sprintf(xorriso->info_text,"Image size %ds exceeds free space on media %ds",
img_sectors + padding, media_space);
if(flag & 1) {
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
} else {
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
{ret= 0; goto ex;}
}
}
if(flag&1) {
ret= multi_emul_blocks + img_sectors + padding;
} else
ret= 1;
ex:;
return(ret);
}
int Xorriso_auto_format(struct XorrisO *xorriso, int flag)
{
int ret, profile, status, num_formats;
char profile_name[80];
struct burn_drive_info *dinfo;
struct burn_drive *drive;
off_t size;
unsigned dummy;
ret= Xorriso_may_burn(xorriso, 0);
if(ret <= 0)
return(0);
ret= Xorriso_get_drive_handles(xorriso, &dinfo, &drive,
"on attempt to autoformat", 2);
if(ret<=0)
return(0);
ret= burn_disc_get_profile(drive, &profile, profile_name);
if(ret>0 && (profile==0x12 || profile==0x43)) { /* DVD-RAM or BD-RE */
ret= burn_disc_get_formats(drive, &status, &size, &dummy, &num_formats);
if(ret>0 && status==BURN_FORMAT_IS_UNFORMATTED) {
sprintf(xorriso->info_text,
"Unformatted %s medium detected. Trying -format fast.",
profile_name);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
ret= Xorriso_format_media(xorriso, (off_t) 0, 1 | 4);
if(ret<=0) {
sprintf(xorriso->info_text, "Automatic formatting of %s failed",
profile_name);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
return(ret);
}
}
}
return(1);
}
int Xorriso_set_system_area(struct XorrisO *xorriso, struct burn_drive *drive,
IsoImage *img, struct isoburn_imgen_opts *sopts,
int flag)
{
int ret, options, system_area_options, iso_lba= -1, start_lba, image_blocks;
char volid[33];
FILE *fp= NULL;
char *buf= NULL, *bufpt= NULL;
off_t hd_lba;
unsigned char *ub;
ElToritoBootImage *bootimg;
IsoFile *bootimg_node;
IsoNode *sparc_core_node;
uint32_t offst;
enum burn_disc_status state;
if(xorriso->grub2_sparc_core[0]) {
ret= Xorriso_node_from_path(xorriso, img, xorriso->grub2_sparc_core,
&sparc_core_node, 1);
if(ret <= 0) {
sprintf(xorriso->info_text,
"Cannot find in ISO image: -boot_image grub grub2_sparc_core=");
Text_shellsafe(xorriso->grub2_sparc_core, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
{ret= 0; goto ex;}
}
if(!ISO_NODE_IS_FILE(sparc_core_node)) {
sprintf(xorriso->info_text,
"Not a data file: -boot_image grub grub2_sparc_core=");
Text_shellsafe(xorriso->grub2_sparc_core, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
{ret= 0; goto ex;}
}
ret = iso_image_set_sparc_core(img, (IsoFile *) sparc_core_node, 0);
if(ret < 0) {
Xorriso_process_msg_queues(xorriso,0);
Xorriso_report_iso_error(xorriso, "", ret,
"Error when setting up -boot_image grub grub2_sparc_core=",
0, "FAILURE", 1);
{ret= 0; goto ex;}
}
}
Xorriso_alloc_meM(buf, char, 32768);
system_area_options= xorriso->system_area_options;
memset(buf, 0, 32768);
if(xorriso->system_area_disk_path[0] == 0) {
if(xorriso->patch_system_area) {
ret= iso_image_get_system_area(img, buf, &options, 0);
if(ret == 0) {
goto do_set;
} else if(ret < 0) {
Xorriso_process_msg_queues(xorriso,0);
Xorriso_report_iso_error(xorriso, "", ret,
"Error when inquiring System Area data of ISO 9660 image",
0, "FAILURE", 1);
{ret= 0; goto ex;}
} else {
system_area_options= xorriso->patch_system_area;
/* Check whether partition 1 ends at image end */;
ub= (unsigned char *) buf;
hd_lba= (ub[454] | (ub[455] << 8) | (ub[456] << 16) | (ub[457] << 24)) +
(ub[458] | (ub[459] << 8) | (ub[460] << 16) | (ub[461] << 24));
iso_lba= -1;
ret= isoburn_disc_get_msc1(drive, &start_lba);
if(ret > 0) {
ret= isoburn_read_iso_head(drive, start_lba, &image_blocks,
volid, 1);
if(ret > 0)
iso_lba= start_lba + image_blocks;
}
if(((off_t) iso_lba) * (off_t) 4 > hd_lba) {
system_area_options= 0;
} else if((xorriso->patch_system_area & 1) &&
((off_t) iso_lba) * (off_t) 4 != hd_lba) {
system_area_options= 0;
} else if((xorriso->patch_system_area & 2) &&
((off_t) iso_lba) * (off_t) 4 + (off_t) (63 * 256) < hd_lba) {
system_area_options= 0;
} else if(xorriso->patch_system_area & 2) { /* isohybrid patching */
/* Check whether bytes 432-345 point to ElTorito LBA */
hd_lba= ub[432] | (ub[433] << 8) | (ub[434] << 16) | (ub[435] << 24);
ret= iso_image_get_boot_image(img, &bootimg, &bootimg_node, NULL);
if(ret != 1)
system_area_options= 0;
else if(bootimg_node != NULL) {
Xorriso__file_start_lba((IsoNode *) bootimg_node, &(iso_lba), 0);
if(((off_t) iso_lba) * (off_t) 4 != hd_lba)
system_area_options= 0;
}
}
if(system_area_options == 0) {
Xorriso_msgs_submit(xorriso, 0,
"Loaded System Area data are not suitable for MBR patching.",
0, "DEBUG", 0);
}
}
bufpt= buf;
ret= 1;
} else
ret= 0;
goto do_set;
}
if(strcmp(xorriso->system_area_disk_path, "/dev/zero") == 0)
{ret= 1; goto do_set;}
ret= Xorriso_afile_fopen(xorriso, xorriso->system_area_disk_path,
"rb", &fp, 2);
if(ret <= 0)
{ret= 0; goto ex;}
ret= fread(buf, 1, 32768, fp);
if(ret < 32768) {
if(ferror(fp)) {
sprintf(xorriso->info_text,
"Error when reading -boot_image system_area=");
Text_shellsafe(xorriso->system_area_disk_path, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, errno, "FAILURE", 0);
{ret= 0; goto ex;}
}
}
bufpt= buf;
do_set:;
if(ret > 0 && xorriso->system_area_disk_path[0]) {
sprintf(xorriso->info_text, "Copying to System Area: %d bytes from file ",
ret);
Text_shellsafe(xorriso->system_area_disk_path, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
}
ret= isoburn_igopt_set_system_area(sopts, bufpt, system_area_options);
if(ret != ISO_SUCCESS) {
Xorriso_process_msg_queues(xorriso,0);
Xorriso_report_iso_error(xorriso, "", ret,
"Error when attaching System Area data to ISO 9660 image",
0, "FAILURE", 1);
{ret= 0; goto ex;}
}
offst= xorriso->partition_offset;
state= isoburn_disc_get_status(drive);
if(state == BURN_DISC_APPENDABLE) {
ret= isoburn_get_img_partition_offset(drive, &offst);
if(ret == 1) {
sprintf(xorriso->info_text,
"Preserving in ISO image: -boot_image any partition_offset=%lu",
(unsigned long) offst);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "NOTE", 0);
} else
offst= xorriso->partition_offset;
}
ret= isoburn_igopt_set_part_offset(sopts, offst,
xorriso->partition_secs_per_head,
xorriso->partition_heads_per_cyl);
if(ret != ISO_SUCCESS) {
Xorriso_process_msg_queues(xorriso,0);
Xorriso_report_iso_error(xorriso, "", ret,
"Error when setting partition offset", 0, "FAILURE", 1);
{ret= 0; goto ex;}
}
ret= 1;
ex:;
if(fp != NULL && fp != stdin)
fclose(fp);
Xorriso_free_meM(buf);
return(ret);
}
/* @param flag bit0= do not increment boot_count
and do not reset boot parameters
bit1= dispose attached boot images
*/
int Xorriso_attach_boot_image(struct XorrisO *xorriso, int flag)
{
int ret;
char *cpt;
struct burn_drive_info *source_dinfo;
struct burn_drive *source_drive;
IsoImage *image= NULL;
IsoNode *node;
ElToritoBootImage *bootimg;
enum eltorito_boot_media_type emul_type= ELTORITO_NO_EMUL;
char *bin_path;
int emul, platform_id;
off_t load_size;
struct stat stbuf;
int hflag= 0;
if(xorriso->boot_image_bin_path[0] == 0 && !(flag & 2)) {
/* >>> no boot image path given : no op */;
ret= 2; goto ex;
}
if(xorriso->in_drive_handle == NULL)
hflag= 2;
ret= Xorriso_get_drive_handles(xorriso, &source_dinfo, &source_drive,
"on attempt to attach boot image", hflag);
if(ret<=0)
goto ex;
image= isoburn_get_attached_image(source_drive);
if(image == NULL) {
/* (should not happen) */
sprintf(xorriso->info_text,
"No ISO image present on attempt to attach boot image");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
ret= 0; goto ex;
}
if(flag & 2) {
iso_image_remove_boot_image(image);
xorriso->boot_count= 0;
ret= 1; goto ex;
}
bin_path= xorriso->boot_image_bin_path;
emul= xorriso->boot_image_emul;
platform_id= xorriso->boot_platform_id;
load_size= xorriso->boot_image_load_size;
if(xorriso->boot_efi_default) {
emul= 0;
platform_id= 0xef;
xorriso->patch_isolinux_image= (xorriso->patch_isolinux_image & ~3) | 0;
}
if(platform_id == 0xef || load_size < 0) {
ret= Xorriso_iso_lstat(xorriso, bin_path, &stbuf, 2 | 4);
if(ret != 0)
{ret= 0; goto ex;}
load_size= ((stbuf.st_size / (off_t) 512) +
!!(stbuf.st_size % (off_t) 512)) * 512;
}
sprintf(xorriso->info_text, "Adding boot image ");
Text_shellsafe(bin_path, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
if(emul == 0)
emul_type= ELTORITO_NO_EMUL;
else if(emul == 1)
emul_type= ELTORITO_HARD_DISC_EMUL;
else if(emul == 2)
emul_type= ELTORITO_FLOPPY_EMUL;
ret= Xorriso_node_from_path(xorriso, image, bin_path, &node, 1);
if(ret <= 0) {
sprintf(xorriso->info_text,
"Cannot find in ISO image: -boot_image ... bin_path=");
Text_shellsafe(bin_path, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
{ret= 0; goto ex;}
}
if(xorriso->boot_count == 0) {
if(xorriso->boot_image_cat_path[0] == 0) {
strcpy(xorriso->boot_image_cat_path, bin_path);
cpt= strrchr(xorriso->boot_image_cat_path, '/');
if(cpt == NULL)
cpt= xorriso->boot_image_cat_path;
else
cpt++;
strcpy(cpt, "boot.cat");
}
ret= Xorriso_node_from_path(xorriso, image, xorriso->boot_image_cat_path,
&node, 1);
if(ret > 0) {
if(!xorriso->do_overwrite) {
sprintf(xorriso->info_text,
"May not overwite existing -boot_image ... cat_path=");
Text_shellsafe(xorriso->boot_image_cat_path, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
{ret= 0; goto ex;}
}
ret= Xorriso_rmi(xorriso, NULL, (off_t) 0, xorriso->boot_image_cat_path,
8 | (xorriso->do_overwrite == 1));
if(ret != 1) {
sprintf(xorriso->info_text,
"Could not remove existing -boot_image cat_path=");
Text_shellsafe(xorriso->boot_image_cat_path, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
{ret= 0; goto ex;}
}
}
/* Discard old boot image, set new one */
ret= iso_image_get_boot_image(image, &bootimg, NULL, NULL);
if(ret == 1)
iso_image_remove_boot_image(image);
ret= iso_image_set_boot_image(image, bin_path, emul_type,
xorriso->boot_image_cat_path, &bootimg);
if(ret > 0)
iso_image_set_boot_catalog_weight(image, 1000000000);
} else {
ret= iso_image_add_boot_image(image, bin_path, emul_type, 0, &bootimg);
}
if(ret < 0) {
Xorriso_process_msg_queues(xorriso,0);
Xorriso_report_iso_error(xorriso, "", ret,
"Error when attaching El-Torito boot image to ISO 9660 image",
0, "FAILURE", 1);
sprintf(xorriso->info_text,
"Could not attach El-Torito boot image to ISO 9660 image");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
{ret= 0; goto ex;}
}
el_torito_set_boot_platform_id(bootimg, (uint8_t) platform_id);
if(load_size / 512 > 65535) {
sprintf(xorriso->info_text,
"Boot image load size exceeds 65535 blocks. Will record 65535 in El Torito.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
load_size= 65535 * 512;
}
el_torito_set_load_size(bootimg, load_size / 512);
el_torito_set_id_string(bootimg, xorriso->boot_id_string);
el_torito_set_selection_crit(bootimg, xorriso->boot_selection_crit);
ret= Xorriso_set_isolinux_options(xorriso, image, 1);
if(!(flag & 1)) {
/* Register attachment and reset even in case of error return */
xorriso->boot_count++;
xorriso->boot_platform_id= 0;
xorriso->patch_isolinux_image= 0;
xorriso->boot_image_bin_path[0]= 0;
xorriso->boot_image_bin_form[0]= 0;
xorriso->boot_image_emul= 0;
xorriso->boot_emul_default= 1;
xorriso->boot_image_load_size= 4 * 512;
memset(xorriso->boot_id_string, 0, sizeof(xorriso->boot_id_string));
memset(xorriso->boot_selection_crit, 0,
sizeof(xorriso->boot_selection_crit));
xorriso->boot_efi_default= 0;
}
if(ret <= 0)
goto ex;
ret= 1;
ex:;
if(image != NULL)
iso_image_unref(image);
return(ret);
}
int Xorriso_write_application_use(struct XorrisO *xorriso,
IsoImage *image, int flag)
{
int l, ret, count= 512;
unsigned int byte= 0;
char *path, data[512];
FILE *fp= NULL;
path= xorriso->application_use;
l= strlen(path);
if(l <= 1) {
memset(data, path[0], 512);
} else if(l == 4 && path[0] == '0' && path[1] == 'x' &&
isxdigit(path[2]) && isxdigit(path[3])) {
sscanf(path + 2, "%x", &byte);
memset(data, (int) byte, 512);
} else {
/* Read up to 512 bytes from file path */
ret= Xorriso_afile_fopen(xorriso, path, "rb", &fp, 0);
if(ret <= 0)
{ret= 0; goto ex;}
ret= fread(data, 1, 512, fp);
if(ret < 512) {
if(ferror(fp)) {
sprintf(xorriso->info_text,
"-application_use: Error while reading file ");
Text_shellsafe(path, xorriso->info_text, 1);
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text,
errno, "FAILURE", 0);
ret= 0; goto ex;
}
}
if(ret < 0)
count= 0;
else
count= ret;
}
iso_image_set_app_use(image, data, count);
ret= 1;
ex:
if(fp != NULL && fp != stdin)
fclose(fp);
return(ret);
}
int Xorriso_retry_write_session(struct XorrisO *xorriso, int flag)
{
int ret, auto_close_mem, do_close_mem;
if(xorriso->do_tao == 1) {
Xorriso_msgs_submit(xorriso, 0,
"There is no hope for a re-try with -close \"on\" as long as -write_type is \"tao\"",
0, "FAILURE", 0);
return(0);
}
Xorriso_msgs_submit(xorriso, 0, "Re-trying with -close \"on\"", 0, "NOTE", 0);
do_close_mem= xorriso->do_close;
auto_close_mem= xorriso->auto_close;
xorriso->do_close= 1;
xorriso->auto_close= 0;
ret= Xorriso_write_session(xorriso, 0);
xorriso->do_close= do_close_mem;
xorriso->auto_close= auto_close_mem;
return(ret);
}
int Xorriso_make_iso_write_opts(struct XorrisO *xorriso, IsoImage *image,
struct isoburn_imgen_opts *sopts, int flag)
{
int ext, i, ret, pad_by_libisofs= 0, is_bootable= 0, relax;
char *out_cs, *part_image;
IsoNode *root_node;
uint32_t padding;
relax= xorriso->relax_compliance;
if(image != NULL)
is_bootable= iso_image_get_boot_image(image, NULL, NULL, NULL);
if(xorriso->patch_isolinux_image & 1) {
if(is_bootable == 1)
relax|= isoburn_igopt_allow_full_ascii;
else if(xorriso->keep_boot_image && is_bootable == 1)
relax|= isoburn_igopt_allow_full_ascii;
}
out_cs= xorriso->out_charset;
if(out_cs == NULL)
Xorriso_get_local_charset(xorriso, &out_cs, 0);
isoburn_igopt_set_level(sopts, xorriso->iso_level);
ext= ((!!xorriso->do_rockridge) * isoburn_igopt_rockridge) |
((!!xorriso->do_joliet) * isoburn_igopt_joliet) |
((!!xorriso->do_hfsplus) * isoburn_igopt_hfsplus) |
((!!xorriso->do_fat) * isoburn_igopt_fat) |
((!!xorriso->do_iso1999) * isoburn_igopt_iso1999) |
(( !(xorriso->ino_behavior & 2)) * isoburn_igopt_hardlinks) |
(( (!(xorriso->ino_behavior & 2)) ||
(xorriso->do_aaip & (2 | 8 | 16 | 256)) ||
(xorriso->do_md5 & (2 | 4)) ||
xorriso->do_hfsplus
) * isoburn_igopt_aaip) |
((!!(xorriso->do_md5 & 2)) * isoburn_igopt_session_md5) |
((!!(xorriso->do_md5 & 4)) * isoburn_igopt_file_md5) |
((!!(xorriso->do_md5 & 8)) * isoburn_igopt_file_stability) |
((!!xorriso->do_old_empty) * isoburn_igopt_old_empty) |
((flag & 1) * isoburn_igopt_will_cancel);
if(xorriso->no_emul_toc & 1)
ext|= isoburn_igopt_no_emul_toc;
isoburn_igopt_set_extensions(sopts, ext);
isoburn_igopt_set_relaxed(sopts, relax);
ret = isoburn_igopt_set_rr_reloc(sopts, xorriso->rr_reloc_dir,
xorriso->rr_reloc_flags);
if(ret <= 0)
{ret= 0; goto ex;}
ret= isoburn_igopt_set_untranslated_name_len(sopts,
xorriso->untranslated_name_len);
if(ret <= 0)
{ret= 0; goto ex;}
isoburn_igopt_set_sort_files(sopts, 1);
isoburn_igopt_set_over_mode(sopts, 0, 0, (mode_t) 0, (mode_t) 0);
isoburn_igopt_set_over_ugid(sopts, 2 * !!xorriso->do_global_uid,
2 * !!xorriso->do_global_gid,
(uid_t) xorriso->global_uid,
(gid_t) xorriso->global_gid);
isoburn_igopt_set_out_charset(sopts, out_cs);
isoburn_igopt_set_fifo_size(sopts, xorriso->fs * 2048);
Ftimetxt(time(NULL), xorriso->scdbackup_tag_time, 8);
isoburn_igopt_set_scdbackup_tag(sopts, xorriso->scdbackup_tag_name,
xorriso->scdbackup_tag_time,
xorriso->scdbackup_tag_written);
if(xorriso->prep_partition[0]) {
ret= isoburn_igopt_set_prep_partition(sopts, xorriso->prep_partition, 0);
if(ret <= 0)
{ret= 0; goto ex;}
}
if(xorriso->efi_boot_partition[0]) {
ret= isoburn_igopt_set_efi_bootp(sopts, xorriso->efi_boot_partition, 0);
if(ret <= 0)
{ret= 0; goto ex;}
}
for(i= 0; i < Xorriso_max_appended_partitionS; i++) {
if(xorriso->appended_partitions[i] == NULL)
continue;
if(xorriso->appended_partitions[i][0] == 0)
continue;
if(strcmp(xorriso->appended_partitions[i], ".") == 0)
part_image= "";
else
part_image= xorriso->appended_partitions[i];
isoburn_igopt_set_partition_img(sopts, i + 1,
xorriso->appended_part_types[i], part_image);
}
isoburn_igopt_set_disc_label(sopts, xorriso->ascii_disc_label);
isoburn_igopt_set_hfsp_serial_number(sopts, xorriso->hfsp_serial_number);
isoburn_igopt_set_hfsp_block_size(sopts, xorriso->hfsp_block_size,
xorriso->apm_block_size);
isoburn_igopt_set_pvd_times(sopts,
xorriso->vol_creation_time, xorriso->vol_modification_time,
xorriso->vol_expiration_time, xorriso->vol_effective_time,
xorriso->vol_uuid);
#ifdef Xorriso_with_libjtE
if(xorriso->libjte_handle && (xorriso->libjte_params_given & (4 | 8))) {
/* >>> Check whether the mandatory parameters are set */;
ret= libjte_set_outfile(xorriso->libjte_handle, xorriso->outdev);
Xorriso_process_msg_queues(xorriso, 0);
if(ret <= 0)
goto ex;
isoburn_igopt_attach_jte(sopts, xorriso->libjte_handle);
pad_by_libisofs= 1;
}
#endif /* Xorriso_with_libjtE */
if(xorriso->do_padding_by_libisofs || pad_by_libisofs) {
/* Padding to be done by libisofs, not by libburn.
*/
padding= xorriso->padding / 2048;
if((uint32_t) xorriso->padding > padding * 2048)
padding++;
/*
fprintf(stderr, "XORRISO_DEBUG: isoburn_igopt_set_tail_blocks(%d)\n",
(int) padding);
*/
isoburn_igopt_set_tail_blocks(sopts, padding);
}
/* Make final abort check before starting expensive activities */
ret= Xorriso_eval_problem_status(xorriso, 1, 0);
if(ret<0)
{ret= 0; goto ex;}
if(xorriso->zisofs_by_magic && image != NULL) {
sprintf(xorriso->info_text,
"Checking disk file content for zisofs compression headers.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "UPDATE", 0);
root_node= (IsoNode *) iso_image_get_root(image);
ret= iso_node_zf_by_magic(root_node,
(xorriso->out_drive_handle == xorriso->in_drive_handle) | 2 | 16);
if(ret<0) {
Xorriso_report_iso_error(xorriso, "", ret,
"Error when examining file content for zisofs headers",
0, "FAILURE", 1);
}
ret= Xorriso_eval_problem_status(xorriso, 1, 0);
if(ret<0)
{ret= 0; goto ex;}
sprintf(xorriso->info_text,
"Check for zisofs compression headers done.");
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "UPDATE", 0);
}
ret = isoburn_igopt_set_write_type(sopts, xorriso->do_tao);
if(ret <= 0)
goto ex;
ret= 1;
ex:;
return(ret);
}
/* @param flag bit0= do not write but only prepare and return size in sectors
@return <=0 error , 1= success
2= failure with DVD-RW, please call Xorriso_retry_write_session()
*/
int Xorriso_write_session(struct XorrisO *xorriso, int flag)
{
int ret, i, pacifier_speed= 0, data_lba, is_bootable= 0;
int freshly_bootable= 0, hide_attr, signal_mode, role;
char *xorriso_id= NULL, *img_id, *sfe= NULL, *out_cs;
struct isoburn_imgen_opts *sopts= NULL;
struct burn_drive_info *dinfo, *source_dinfo;
struct burn_drive *drive, *source_drive;
struct burn_disc *disc= NULL;
struct burn_write_opts *burn_options= NULL;
off_t readcounter= 0,writecounter= 0;
int num_sessions= 0, num_tracks= 0;
struct burn_session **sessions;
struct burn_track **tracks;
enum burn_disc_status s;
struct burn_multi_caps *caps= NULL;
IsoImage *image= NULL;
int profile_number;
char *profile_name= NULL, *reasons= NULL;
IsoBoot *bootcat_node;
Xorriso_alloc_meM(sfe, char, 5 * SfileadrL);
Xorriso_alloc_meM(xorriso_id, char, 256);
Xorriso_alloc_meM(profile_name, char,