static void write_finalize( filter_t *p_filter, uint16_t i_data_type, uint8_t i_length_mul ) { filter_sys_t *p_sys = p_filter->p_sys; assert( p_sys->p_out_buf != NULL ); uint8_t *p_out = p_sys->p_out_buf->p_buffer; /* S/PDIF header */ if( i_data_type != 0 ) { assert( p_sys->i_out_offset > SPDIF_HEADER_SIZE ); assert( i_length_mul == 1 || i_length_mul == 8 ); set_16( p_filter, &p_out[0], 0xf872 ); /* syncword 1 */ set_16( p_filter, &p_out[2], 0x4e1f ); /* syncword 2 */ set_16( p_filter, &p_out[4], i_data_type ); /* data type */ /* length in bits or bytes */ set_16( p_filter, &p_out[6], ( p_sys->i_out_offset - SPDIF_HEADER_SIZE ) * i_length_mul ); } /* 0 padding */ if( p_sys->i_out_offset < p_sys->p_out_buf->i_buffer ) write_padding( p_filter, p_sys->p_out_buf->i_buffer - p_sys->i_out_offset ); }
static void write_data( filter_t *p_filter, const void *p_buf, size_t i_size, bool b_input_big_endian ) { filter_sys_t *p_sys = p_filter->p_sys; assert( p_sys->p_out_buf != NULL ); bool b_output_big_endian = p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB; uint8_t *p_out = &p_sys->p_out_buf->p_buffer[p_sys->i_out_offset]; const uint8_t *p_in = p_buf; assert( p_sys->p_out_buf->i_buffer - p_sys->i_out_offset >= i_size ); if( b_input_big_endian != b_output_big_endian ) swab( p_in, p_out, i_size & ~1 ); else memcpy( p_out, p_in, i_size & ~1 ); p_sys->i_out_offset += ( i_size & ~1 ); if( i_size & 1 ) { assert( p_sys->p_out_buf->i_buffer - p_sys->i_out_offset >= 2 ); p_out += ( i_size & ~1 ); set_16( p_filter, p_out, p_in[i_size - 1] << 8 ); p_sys->i_out_offset += 2; } }
static void write_16( filter_t *p_filter, uint16_t i_val ) { filter_sys_t *p_sys = p_filter->p_sys; assert( p_sys->p_out_buf != NULL ); assert( p_sys->p_out_buf->i_buffer - p_sys->i_out_offset >= sizeof( uint16_t ) ); set_16( p_filter, &p_sys->p_out_buf->p_buffer[p_sys->i_out_offset], i_val ); p_sys->i_out_offset += sizeof( uint16_t ); }
/* * This patches the boot sector and the beginning of ldlinux.sys * based on an ldlinux.sys sector map passed in. Typically this is * handled by writing ldlinux.sys, mapping it, and then overwrite it * with the patched version. If this isn't safe to do because of * an OS which does block reallocation, then overwrite it with * direct access since the location is known. * * Returns the number of modified bytes in ldlinux.sys if successful, * otherwise -1. */ int syslinux_patch(const sector_t *sectp, int nsectors, int stupid, int raid_mode, const char *subdir, const char *subvol) { struct patch_area *patcharea; struct ext_patch_area *epa; struct syslinux_extent *ex; uint32_t *wp; int nsect = ((boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + 2; uint32_t csum; int i, dw, nptrs; struct fat_boot_sector *sbs = (struct fat_boot_sector *)boot_sector; uint64_t *advptrs; if (nsectors < nsect) return -1; /* The actual file is too small for content */ /* Search for LDLINUX_MAGIC to find the patch area */ for (wp = (uint32_t *)boot_image; get_32_sl(wp) != LDLINUX_MAGIC; wp++) ; patcharea = (struct patch_area *)wp; epa = ptr(boot_image, &patcharea->epaoffset); /* First sector need pointer in boot sector */ set_32(ptr(sbs, &epa->sect1ptr0), sectp[0]); set_32(ptr(sbs, &epa->sect1ptr1), sectp[0] >> 32); sectp++; /* Handle RAID mode */ if (raid_mode) { /* Patch in INT 18h = CD 18 */ set_16(ptr(sbs, &epa->raidpatch), 0x18CD); } /* Set up the totals */ dw = boot_image_len >> 2; /* COMPLETE dwords, excluding ADV */ set_16_sl(&patcharea->data_sectors, nsect - 2); /* Not including ADVs */ set_16_sl(&patcharea->adv_sectors, 2); /* ADVs need 2 sectors */ set_32_sl(&patcharea->dwords, dw); /* Handle Stupid mode */ if (stupid) { /* Access only one sector at a time */ set_16_sl(&patcharea->maxtransfer, 1); } /* Set the sector extents */ ex = ptr(boot_image, &epa->secptroffset); nptrs = get_16_sl(&epa->secptrcnt); if (nsect > nptrs) { /* Not necessarily an error in this case, but a general problem */ fprintf(stderr, "Insufficient extent space, build error!\n"); exit(1); } /* -1 for the pointer in the boot sector, -2 for the two ADVs */ generate_extents(ex, nptrs, sectp, nsect-1-2); /* ADV pointers */ advptrs = ptr(boot_image, &epa->advptroffset); set_64_sl(&advptrs[0], sectp[nsect-1-2]); set_64_sl(&advptrs[1], sectp[nsect-1-1]); /* Poke in the base directory path */ if (subdir) { int sublen = strlen(subdir) + 1; if (get_16_sl(&epa->dirlen) < sublen) { fprintf(stderr, "Subdirectory path too long... aborting install!\n"); exit(1); } memcpy_to_sl(ptr(boot_image, &epa->diroffset), subdir, sublen); } /* Poke in the subvolume information */ if (subvol) { int sublen = strlen(subvol) + 1; if (get_16_sl(&epa->subvollen) < sublen) { fprintf(stderr, "Subvol name too long... aborting install!\n"); exit(1); } memcpy_to_sl(ptr(boot_image, &epa->subvoloffset), subvol, sublen); } /* Now produce a checksum */ set_32_sl(&patcharea->checksum, 0); csum = LDLINUX_MAGIC; for (i = 0, wp = (uint32_t *)boot_image; i < dw; i++, wp++) csum -= get_32_sl(wp); /* Negative checksum */ set_32_sl(&patcharea->checksum, csum); /* * Assume all bytes modified. This can be optimized at the expense * of keeping track of what the highest modified address ever was. */ return dw << 2; }