3 err
() { printf 'error: %b\n' "$*" 1>&2; exit 1; }
6 backup-dvd [options] [commands]
9 -i <input dev> Defaults to ${dev}
10 -o <output dir> Defaults to ${out}
11 -n <volume name> Defaults to volume on disk
14 --errors Image the raw disc first w/ddrescue
15 --backup Back up the disc
16 --modify Tweak runtime settings (needs -n)
17 --mkiso Create an iso (needs -n)
19 If no commands are specified, then all are run.
28 unset doit_
{backup
,modify
,mkiso
}
31 eval set -- `getopt -l errors,backup,modify,mkiso -- hi:n:o: "$@"`
32 while [[ -n $1 ]] ; do
39 --errors) doit_errors
=true
; shift;;
40 --backup) doit_backup
=true
; shift;;
41 --modify) doit_modify
=true
; shift;;
42 --mkiso) doit_mkiso
=true
; shift;;
55 if [[ -z ${doit_backup}${doit_modify}${doit_mkiso} ]] ; then
64 Publisher
= # -publisher
69 if ${doit_backup} ; then
70 if [[ -z ${Volume} ]] ; then
71 info
=$
(iso-info
${dev}) ||
exit 1
73 eval $
(echo "${info}" |
awk -F: '
74 (NF > 1 && $1 !~ /image/) {
78 print $1 "=\"" $2 "\"";
82 if [[ -z ${Volume} ]] ; then
83 echo "Unable to parse Volume out of ISO"
87 for v
in Application Preparer Publisher System Volume Volume_Set
; do
89 done > "${out}/.${Volume}.vars.sh"
91 [[ -z ${Volume} ]] && err
"Need to specify -n with --modify/--mkiso"
92 .
"${out}/.${Volume}.vars.sh" ||
exit 1
97 [[ ${a} == *" "* ||
${#a} == 0 ]] && fmt='"%s"' ||
fmt='%s'
98 printf "${fmt} " "${a}"
105 # Mirror the disc w/ddrescue which tolerates errors.
109 local raw
=".${Volume}.raw"
110 local log
="${raw}.log"
112 -v -b 2048 ${dev} "${raw}" "${log}"
114 ddrescue -p -n "${opts[@]}"
115 ddrescue -d -r 3 "${opts[@]}"
116 ddrescue -d -r 3 -R "${opts[@]}"
124 echo "Backing up
: ${Volume}"
130 if ! e dvdbackup -M "${opts[@]}" ; then
131 e dvdbackup -F "${opts[@]}" || exit 1
136 # Modify some of the runtime settings.
140 while [[ $# -gt 3 ]]; do
150 local file=$1 off=$2 num=${3:-1}
151 hexdump -v -n ${num} -s $((${off})) -e "1/1 \"%02x${sep}\"" "${file}"
156 (for b
; do printf "\x${b#0x}"; done) | \
158 bs
=1 seek
=$
((${off})) \
159 conv
=notrunc status
=none
162 # DVDs have sectors of 2048 bytes (2^11).
163 local file=$1 off
=$2 num
=${3:-4}
164 echo $
(( $
(bytes_get
"${file}" "${off}" "${num}") << 11 ))
168 # Back up the .IFO files before we edit them.
169 local ifo=$1 magic=$2
170 [[ -e ${ifo} ]] || return 1
172 if [[ $(bytes_get "${ifo}" 0 12) != "0x${magic}" ]] ; then
173 echo "not a VMG IFO file: ${ifo}"
176 if [[ ! -e ${ifo}.bak ]] ; then
177 cp -a "${ifo}" "${ifo}.bak"
184 [[ -e ifodump ]] || return 0
187 ./ifodump -f "${file}.bak" > "${file}.bak.dmp"
188 ./ifodump -f "${file}" > "${file}.dmp"
189 diff -u "${file}.bak.dmp" "${file}.dmp" | sed -e 1d -e 2d > "${file}.diff"
191 grep '^[+-]' "${file}.diff" | \
193 -e '^-VMG Category:.*' \
194 -e '^+VMG Category: 00000000 (Region Code=ff)' \
195 -e '^[-+].*Title playback type:' \
196 -e '^-.*Title or time play:1' \
197 -e '^+.*Title or time play:0' \
198 -e '^-Prohibited user operations:' \
199 -e '^+Prohibited user operations: Angle Change, $' \
200 -e '^+Prohibited user operations: None$' \
201 | egrep -v -e '^[-+]([0-9a-f]{2} )+$'
203 rm "${file}".{{,bak.}dmp,diff}
204 if [[ -z ${out} ]] ; then
207 echo "error in ifo modification:"
214 # http://dvdnav.mplayerhq.hu/dvdinfo/pgc.html
215 local file=$1 pgci_off=$2 indent=$3
216 local num_pgcs p pgc_off off
218 num_pgcs=$(bytes_get "${file}" ${pgci_off} 2)
219 for (( p = 0; p < num_pgcs; ++p )) ; do
222 $(bytes_get "${file}" $(( pgci_off + 8 + 8 * p + 4 )) 4)
224 off=$(( pgc_off + 8 ))
225 pgc_puo=$(bytes_get "${file}" ${off} 4)
226 printf '%bpgc #%2i @ %#06x: %s: ' "${indent}" ${p} ${vmgm_pgc_off} ${pgc_puo}
227 new_pgc_puo=$(( pgc_puo & (1 << 22) )) # Keep angle field.
228 if [[ ${pgc_puo} -ne ${new_pgc_puo} ]] ; then
229 bytes_set "${file}" ${off} 0 $(printf '%x' $(( new_pgc_puo >> 16 ))) 0 0
238 # http://dvdnav.mplayerhq.hu/dvdinfo/ifo_vts.html#pgciut
239 local file=$1 pgci_ut_off=$2
240 local num_langs l off
242 num_langs=$(bytes_get "${file}" ${pgci_ut_off} 2)
243 for (( l = 0; l < num_langs; ++l )) ; do
246 $(bytes_get "${file}" $(( pgci_ut_off + 8 + 8 * l + 4 )) 4)
248 printf '\tclearing lang #%i @ %#06x\n' ${l} ${off}
249 process_pgci "${file}" ${off} '\t\t'
254 # Back up the .IFO files before we edit them.
256 _check_ifo "${vmgi}" "445644564944454f2d564d47" || return 1
260 # Clear the region code.
261 # http://dvdnav.mplayerhq.hu/dvdinfo/ifo.html
262 # 0x23 - region code restrict byte - set to 0x00 for region free.
263 echo "clearing region code"
264 bytes_set "${vmgi}" 0x23 0x00
266 # Clear all Prohibited User Operations (PUOs) in TT_SRPT (Uop1 & Uop0).
267 # http://dvdnav.mplayerhq.hu/dvdinfo/ifo_vmg.html
268 echo "clearing PUOs in TT_SRPT"
269 tt_srpt_sec=$(dvdsec "${vmgi}" 0xC4)
271 num_titles=$(bytes_get "${vmgi}" ${tt_srpt_sec} 2)
272 for (( t = 0; t < num_titles; ++t )) ; do
273 off=$(( tt_srpt_sec + 8 + (t * 12) ))
274 printf "\ttitle type #%2i @ %#06x: " "${t}" "${off}"
275 title_type=$(bytes_get "${vmgi}" ${off} 1)
276 uop0=$(( title_type & 1 ))
277 uop1=$(( title_type & 2 ))
278 printf "%s (uop1:%i uop0:%i): " ${title_type} ${uop1} ${uop0}
279 new_title_type=$(( title_type & ~3 ))
280 if [[ ${title_type} -ne ${new_title_type} ]] ; then
281 bytes_set "${vmgi}" ${off} $(printf '%x' ${new_title_type})
288 # Clear all the PUOs in the PGCs. But Preserve the angle bit.
289 # Note: Removal of the angle PUO can cause certain standalone players to
290 # display the "angle" icon during playback. Removal of the angle PUO
291 # whilst permitted is therefore not recommended.
292 # http://dvdnav.mplayerhq.hu/dvdinfo/uops.html
293 #fp_pgc_addr=$(bytes_get "${vmgi}" 0x84 4)
294 #num_pgcs=$(bytes_get "${vmgi}" $(( fp_pgc_sec + 1 )) 1)
295 echo "clearing PUOs in PGCs"
296 vmgm_pgci_ut_sec=$(dvdsec "${vmgi}" 0xC8)
297 process_pgci_ut "${vmgi}" ${vmgm_pgci_ut_sec}
301 # Back up the .IFO files before we edit them.
303 _check_ifo "${vtsi}" "445644564944454f2d565453" || return 1
307 echo "clearing PUOs in PGCs"
309 vts_pgci_sec=$(dvdsec "${vtsi}" 0xCC)
310 process_pgci "${vtsi}" ${vts_pgci_sec} '\t'
312 vtsm_pgci_ut_sec=$(dvdsec "${vtsi}" 0xD0)
313 process_pgci_ut "${vtsi}" ${vtsm_pgci_ut_sec}
317 local out_vts_dir="${out}/${Volume}/VIDEO_TS"
320 if ${doit_errors} ; then
321 echo "skipping modifications due to possible errors in files"
325 for vmgi in "${out_vts_dir}"/VIDEO_TS.{IFO,BUP} ; do
326 process_vmgi "${vmgi}"
327 check_changes "${vmgi}" || exit 1
330 for vtsi in "${out_vts_dir}"/VTS_??_?.{IFO,BUP} ; do
331 process_vtsi "${vtsi}"
332 check_changes "${vtsi}" || exit 1
337 # Finally, create a new iso.
340 set -- mkisofs -quiet -dvd-video -no-bak \
341 -A "${Application}" \
343 -publisher "${Publisher}" \
346 -volset "${Volume_Set}" \
347 -o "${Volume}.iso" "${Volume}"
348 sh="${out}/.${Volume}.sh"
351 echo "cd '${out}' || exit 1"
352 echo ". './.${Volume}.vars.sh'"
356 echo 'echo "$@"; exec "$@"'
361 du -h "${out}/${Volume}.iso"
362 sudo chattr +i "${out}/${Volume}.iso"
365 ${doit_errors} && raw_read_dvd
366 ${doit_backup} && backup_dvd
367 ${doit_modify} && modify_dvd
368 ${doit_mkiso} && mkiso_dvd
370 ${doit_backup} && eject