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.

953 lines
22 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. /* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
  2. /* Copyright (c) 2004 - 2006 Derek Foreman, Ben Jansens
  3. Copyright (c) 2006 - 2016 Thomas Schmitt <scdbackup@gmx.net>
  4. Provided under GPL version 2 or later.
  5. */
  6. #ifdef HAVE_CONFIG_H
  7. #include "../config.h"
  8. #endif
  9. #include <stdio.h>
  10. /* ts A61010 */
  11. /* #include <a ssert.h> */
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include "error.h"
  15. #include "options.h"
  16. #include "transport.h"
  17. #include "libburn.h"
  18. #include "drive.h"
  19. #include "sector.h"
  20. #include "crc.h"
  21. #include "debug.h"
  22. #include "toc.h"
  23. #include "write.h"
  24. #include "libdax_msgs.h"
  25. extern struct libdax_msgs *libdax_messenger;
  26. #include "ecma130ab.h"
  27. #ifdef Libburn_log_in_and_out_streaM
  28. /* ts A61031 */
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #endif /* Libburn_log_in_and_out_streaM */
  33. /* ts B41126 : O_BINARY is needed for Cygwin but undefined elsewhere */
  34. #ifndef O_BINARY
  35. #define O_BINARY 0
  36. #endif
  37. /*static unsigned char isrc[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";*/
  38. #define sector_common(X) d->alba++; d->rlba X;
  39. static void uncook_subs(unsigned char *dest, unsigned char *source)
  40. {
  41. int i, j, code;
  42. memset(dest, 0, 96);
  43. for (i = 0; i < 12; i++) {
  44. for (j = 0; j < 8; j++) {
  45. for (code = 0; code < 8; code++) {
  46. if (source[code * 12 + i] & 0x80)
  47. dest[j + i * 8] |= (1 << (7 - code));
  48. source[code * 12 + i] <<= 1;
  49. }
  50. }
  51. }
  52. }
  53. /* @return >=0 : valid , <0 invalid */
  54. int sector_get_outmode(enum burn_write_types write_type,
  55. enum burn_block_types block_type)
  56. {
  57. /* ts A61103 : extended SAO condition to TAO */
  58. if (write_type == BURN_WRITE_SAO || write_type == BURN_WRITE_TAO)
  59. return 0;
  60. else
  61. switch (block_type) {
  62. case BURN_BLOCK_RAW0:
  63. return BURN_MODE_RAW;
  64. case BURN_BLOCK_RAW16:
  65. return BURN_MODE_RAW | BURN_SUBCODE_P16;
  66. case BURN_BLOCK_RAW96P:
  67. return BURN_MODE_RAW | BURN_SUBCODE_P96;
  68. case BURN_BLOCK_RAW96R:
  69. return BURN_MODE_RAW | BURN_SUBCODE_R96;
  70. case BURN_BLOCK_MODE1:
  71. return BURN_MODE1;
  72. default:
  73. return -1;
  74. }
  75. /* ts A61007 : now handled in burn_write_opts_set_write_type() */
  76. /* a ssert(0); */ /* return BURN_MODE_UNIMPLEMENTED :) */
  77. }
  78. /* 0 means "same as inmode" */
  79. static int get_outmode(struct burn_write_opts *o)
  80. {
  81. /* ts A61007 */
  82. return sector_get_outmode(o->write_type, o->block_type);
  83. /* -1 is prevented by check in burn_write_opts_set_write_type() */
  84. /* a ssert(0); */ /* return BURN_MODE_UNIMPLEMENTED :) */
  85. }
  86. static void get_bytes(struct burn_track *track, int count, unsigned char *data)
  87. {
  88. int valid, shortage, curr, i, tr;
  89. #ifdef Libburn_log_in_and_out_streaM
  90. /* ts A61031 */
  91. static int tee_fd= -1;
  92. if(tee_fd==-1)
  93. tee_fd= open("/tmp/libburn_sg_readin",
  94. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
  95. S_IRUSR | S_IWUSR);
  96. #endif /* Libburn_log_in_and_out_streaM */
  97. /* no track pointer means we're just generating 0s */
  98. if (!track) {
  99. memset(data, 0, count);
  100. return;
  101. }
  102. /* first we use up any offset */
  103. valid = track->offset - track->offsetcount;
  104. if (valid > count)
  105. valid = count;
  106. if (valid) {
  107. track->offsetcount += valid;
  108. memset(data, 0, valid);
  109. }
  110. shortage = count - valid;
  111. if (!shortage)
  112. goto ex;
  113. /* Next we use source data */
  114. curr = valid;
  115. if (!track->eos) {
  116. if (track->source->read != NULL)
  117. valid = track->source->read(track->source,
  118. data + curr, count - curr);
  119. else
  120. valid = track->source->read_xt(track->source,
  121. data + curr, count - curr);
  122. } else valid = 0;
  123. if (valid <= 0) { /* ts A61031 : extended from (valid == -1) */
  124. track->eos = 1;
  125. valid = 0;
  126. }
  127. track->sourcecount += valid;
  128. #ifdef Libburn_log_in_and_out_streaM
  129. /* ts A61031 */
  130. if(tee_fd!=-1 && valid>0) {
  131. write(tee_fd, data + curr, valid);
  132. }
  133. #endif /* Libburn_log_in_and_out_streaM */
  134. curr += valid;
  135. shortage = count - curr;
  136. if (!shortage)
  137. goto ex;
  138. /* Before going to the next track, we run through any tail */
  139. valid = track->tail - track->tailcount;
  140. if (valid > count - curr)
  141. valid = count - curr;
  142. if (valid) {
  143. track->tailcount += valid;
  144. memset(data + curr, 0, valid);
  145. }
  146. curr += valid;
  147. shortage -= valid;
  148. if (!shortage)
  149. goto ex;
  150. /* ts A61031 - B10103 */
  151. if (shortage >= count)
  152. track->track_data_done = 1;
  153. if (track->end_on_premature_eoi && shortage >= count &&
  154. !track->open_ended) {
  155. char msg[80];
  156. off_t missing, inp_block_size, track_blocks;
  157. inp_block_size = burn_sector_length(track->mode);
  158. track_blocks = burn_track_get_sectors_2(track, 1);
  159. missing = track_blocks * inp_block_size - track->sourcecount;
  160. sprintf(msg,
  161. "Premature end of input encountered. Missing: %.f bytes",
  162. (double) missing);
  163. libdax_msgs_submit(libdax_messenger, -1, 0x00020180,
  164. LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  165. msg, 0,0);
  166. /* Memorize that premature end of input happened */
  167. track->end_on_premature_eoi = 2;
  168. }
  169. if (track->open_ended || track->end_on_premature_eoi)
  170. goto ex;
  171. /* If we're still short, and there's a "next" pointer, we pull from that.
  172. if that depletes, we'll just fill with 0s.
  173. */
  174. if (track->source->next) {
  175. struct burn_source *src;
  176. printf("pulling from next track\n");
  177. src = track->source->next;
  178. valid = src->read(src, data + curr, shortage);
  179. if (valid > 0) {
  180. shortage -= valid;
  181. curr += valid;
  182. }
  183. }
  184. ex:;
  185. /* ts A61024 : general finalizing processing */
  186. if(shortage)
  187. memset(data + curr, 0, shortage); /* this is old icculus.org */
  188. if (track->swap_source_bytes == 1) {
  189. for (i = 1; i < count; i += 2) {
  190. tr = data[i];
  191. data[i] = data[i-1];
  192. data[i-1] = tr;
  193. }
  194. }
  195. }
  196. /* ts B20113 : outsourced from get_sector() */
  197. int sector_write_buffer(struct burn_drive *d,
  198. struct burn_track *track, int flag)
  199. {
  200. int err, i;
  201. struct buffer *out;
  202. out = d->buffer;
  203. if (out->sectors <= 0)
  204. return 2;
  205. err = d->write(d, d->nwa, out);
  206. if (err == BE_CANCELLED)
  207. return 0;
  208. /* ts A61101 */
  209. if(track != NULL) {
  210. track->writecount += out->bytes;
  211. track->written_sectors += out->sectors;
  212. /* Determine current index */
  213. for (i = d->progress.index; i + 1 < track->indices; i++) {
  214. if (track->index[i + 1] > d->nwa + out->sectors)
  215. break;
  216. d->progress.index = i + 1;
  217. }
  218. }
  219. /* ts A61119 */
  220. d->progress.buffered_bytes += out->bytes;
  221. d->nwa += out->sectors;
  222. out->bytes = 0;
  223. out->sectors = 0;
  224. return 1;
  225. }
  226. /* ts A61009 : seems to hand out sector start pointer in opts->drive->buffer
  227. and to count hand outs as well as reserved bytes */
  228. /* ts A61101 : added parameter track for counting written bytes */
  229. static unsigned char *get_sector(struct burn_write_opts *opts,
  230. struct burn_track *track, int inmode)
  231. {
  232. struct burn_drive *d = opts->drive;
  233. struct buffer *out = d->buffer;
  234. int outmode, seclen, write_ret;
  235. unsigned char *ret;
  236. outmode = get_outmode(opts);
  237. if (outmode == 0)
  238. outmode = inmode;
  239. /* ts A61009 : react on eventual failure of burn_sector_length()
  240. (should not happen if API tested properly).
  241. Ensures out->bytes >= out->sectors */
  242. seclen = burn_sector_length(outmode);
  243. if (seclen <= 0)
  244. return NULL;
  245. seclen += burn_subcode_length(outmode);
  246. /* ts A61219 : opts->obs is eventually a 32k trigger for DVD */
  247. /* (there is enough buffer size reserve for track->cdxa_conversion) */
  248. if (out->bytes + seclen > BUFFER_SIZE ||
  249. (opts->obs > 0 && out->bytes + seclen > opts->obs)) {
  250. write_ret = sector_write_buffer(d, track, 0);
  251. if (write_ret <= 0)
  252. return NULL;
  253. }
  254. ret = out->data + out->bytes;
  255. out->bytes += seclen;
  256. out->sectors++;
  257. return ret;
  258. }
  259. /* ts A61031 */
  260. /* Revoke the counting of the most recent sector handed out by get_sector() */
  261. static void unget_sector(struct burn_write_opts *opts, int inmode)
  262. {
  263. struct burn_drive *d = opts->drive;
  264. struct buffer *out = d->buffer;
  265. int outmode;
  266. int seclen;
  267. outmode = get_outmode(opts);
  268. if (outmode == 0)
  269. outmode = inmode;
  270. /* ts A61009 : react on eventual failure of burn_sector_length()
  271. (should not happen if API tested properly).
  272. Ensures out->bytes >= out->sectors */
  273. seclen = burn_sector_length(outmode);
  274. if (seclen <= 0)
  275. return;
  276. seclen += burn_subcode_length(outmode);
  277. out->bytes -= seclen;
  278. out->sectors--;
  279. }
  280. /* either inmode == outmode, or outmode == raw. anything else is bad news */
  281. /* ts A61010 : changed type to int in order to propagate said bad news */
  282. /** @return 1 is ok, <= 0 is failure */
  283. static int convert_data(struct burn_write_opts *o, struct burn_track *track,
  284. int inmode, unsigned char *data)
  285. {
  286. int outlen, inlen;
  287. int offset = -1;
  288. int outmode;
  289. outmode = get_outmode(o);
  290. if (outmode == 0)
  291. outmode = inmode;
  292. outlen = burn_sector_length(outmode);
  293. inlen = burn_sector_length(inmode);
  294. /* ts A61010 */
  295. /* a ssert(outlen >= inlen); */
  296. if (outlen < inlen || outlen < 0 || inlen < 0)
  297. return 0;
  298. if ((outmode & BURN_MODE_BITS) == (inmode & BURN_MODE_BITS)) {
  299. /* see MMC-5 4.2.3.8.5.3 Block Format for Mode 2 form 1 Data
  300. Table 24 Mode 2 Formed Sector Sub-header Format */
  301. if (track != NULL)
  302. if (track->cdxa_conversion == 1)
  303. inlen += 8;
  304. get_bytes(track, inlen, data);
  305. if (track != NULL)
  306. if (track->cdxa_conversion == 1)
  307. memmove(data, data + 8, inlen - 8);
  308. return 1;
  309. }
  310. /* ts A61010 */
  311. /* a ssert(outmode & BURN_MODE_RAW); */
  312. if (!(outmode & BURN_MODE_RAW))
  313. return 0;
  314. if (inmode & BURN_MODE1)
  315. offset = 16;
  316. if (inmode & BURN_MODE_RAW)
  317. offset = 0;
  318. if (inmode & BURN_AUDIO)
  319. offset = 0;
  320. /* ts A61010 */
  321. /* a ssert(offset != -1); */
  322. if (offset == -1)
  323. return 0;
  324. get_bytes(track, inlen, data + offset);
  325. return 1;
  326. }
  327. static void convert_subs(struct burn_write_opts *o, int inmode,
  328. unsigned char *subs, unsigned char *sector)
  329. {
  330. unsigned char *out;
  331. int outmode;
  332. outmode = get_outmode(o);
  333. if (outmode == 0)
  334. outmode = inmode;
  335. sector += burn_sector_length(outmode);
  336. /* XXX for sao with subs, we'd need something else... */
  337. switch (o->block_type) {
  338. case BURN_BLOCK_RAW96R:
  339. uncook_subs(sector, subs);
  340. break;
  341. case BURN_BLOCK_RAW16:
  342. memcpy(sector, subs + 12, 12);
  343. out = sector + 12;
  344. out[0] = 0;
  345. out[1] = 0;
  346. out[2] = 0;
  347. /*XXX find a better way to deal with partially damaged P channels*/
  348. if (subs[2] != 0)
  349. out[3] = 0x80;
  350. else
  351. out[3] = 0;
  352. out = sector + 10;
  353. out[0] = ~out[0];
  354. out[1] = ~out[1];
  355. break;
  356. /* ts A61119 : to silence compiler warnings */
  357. default:;
  358. }
  359. }
  360. static void subcode_toc(struct burn_drive *d, int mode, unsigned char *data)
  361. {
  362. unsigned char *q;
  363. int track;
  364. int crc;
  365. int min, sec, frame;
  366. track = d->toc_temp / 3;
  367. memset(data, 0, 96);
  368. q = data + 12;
  369. burn_lba_to_msf(d->rlba, &min, &sec, &frame);
  370. /*XXX track numbers are BCD
  371. a0 - 1st track ctrl
  372. a1 - last track ctrl
  373. a2 - lout ctrl
  374. */
  375. q[0] = (d->toc_entry[track].control << 4) + 1;
  376. q[1] = 0;
  377. if (d->toc_entry[track].point < 100)
  378. q[2] = dec_to_bcd(d->toc_entry[track].point);
  379. else
  380. q[2] = d->toc_entry[track].point;
  381. q[3] = dec_to_bcd(min);
  382. q[4] = dec_to_bcd(sec);
  383. q[5] = dec_to_bcd(frame);
  384. q[6] = 0;
  385. q[7] = dec_to_bcd(d->toc_entry[track].pmin);
  386. q[8] = dec_to_bcd(d->toc_entry[track].psec);
  387. q[9] = dec_to_bcd(d->toc_entry[track].pframe);
  388. #ifdef Libburn_no_crc_C
  389. crc = 0; /* dummy */
  390. #else
  391. crc = crc_ccitt(q, 10);
  392. #endif
  393. q[10] = crc >> 8;
  394. q[11] = crc & 0xFF;
  395. d->toc_temp++;
  396. d->toc_temp %= (d->toc_entries * 3);
  397. }
  398. int sector_toc(struct burn_write_opts *o, int mode)
  399. {
  400. struct burn_drive *d = o->drive;
  401. unsigned char *data;
  402. unsigned char subs[96];
  403. data = get_sector(o, NULL, mode);
  404. if (data == NULL)
  405. return 0;
  406. /* ts A61010 */
  407. if (convert_data(o, NULL, mode, data) <= 0)
  408. return 0;
  409. subcode_toc(d, mode, subs);
  410. convert_subs(o, mode, subs, data);
  411. if (sector_headers(o, data, mode, 1) <= 0)
  412. return 0;
  413. sector_common(++)
  414. return 1;
  415. }
  416. int sector_pregap(struct burn_write_opts *o,
  417. unsigned char tno, unsigned char control, int mode)
  418. {
  419. struct burn_drive *d = o->drive;
  420. unsigned char *data;
  421. unsigned char subs[96];
  422. data = get_sector(o, NULL, mode);
  423. if (data == NULL)
  424. return 0;
  425. /* ts A61010 */
  426. if (convert_data(o, NULL, mode, data) <= 0)
  427. return 0;
  428. subcode_user(o, subs, tno, control, 0, NULL, 1);
  429. convert_subs(o, mode, subs, data);
  430. if (sector_headers(o, data, mode, 0) <= 0)
  431. return 0;
  432. sector_common(--)
  433. return 1;
  434. }
  435. int sector_postgap(struct burn_write_opts *o,
  436. unsigned char tno, unsigned char control, int mode)
  437. {
  438. struct burn_drive *d = o->drive;
  439. unsigned char subs[96];
  440. unsigned char *data;
  441. data = get_sector(o, NULL, mode);
  442. if (data == NULL)
  443. return 0;
  444. /* ts A61010 */
  445. if (convert_data(o, NULL, mode, data) <= 0)
  446. return 0;
  447. /* use last index in track */
  448. subcode_user(o, subs, tno, control, 1, NULL, 1);
  449. convert_subs(o, mode, subs, data);
  450. if (sector_headers(o, data, mode, 0) <= 0)
  451. return 0;
  452. sector_common(++)
  453. return 1;
  454. }
  455. static void subcode_lout(struct burn_write_opts *o, unsigned char control,
  456. unsigned char *data)
  457. {
  458. struct burn_drive *d = o->drive;
  459. unsigned char *q;
  460. int crc;
  461. int rmin, min, rsec, sec, rframe, frame;
  462. memset(data, 0, 96);
  463. q = data + 12;
  464. burn_lba_to_msf(d->alba, &min, &sec, &frame);
  465. burn_lba_to_msf(d->rlba, &rmin, &rsec, &rframe);
  466. if (((rmin == 0) && (rsec == 0) && (rframe == 0)) ||
  467. ((rsec >= 2) && !((rframe / 19) % 2)))
  468. memset(data, 0xFF, 12);
  469. q[0] = (control << 4) + 1;
  470. q[1] = 0xAA;
  471. q[2] = 0x01;
  472. q[3] = dec_to_bcd(rmin);
  473. q[4] = dec_to_bcd(rsec);
  474. q[5] = dec_to_bcd(rframe);
  475. q[6] = 0;
  476. q[7] = dec_to_bcd(min);
  477. q[8] = dec_to_bcd(sec);
  478. q[9] = dec_to_bcd(frame);
  479. #ifdef Libburn_no_crc_C
  480. crc = 0; /* dummy */
  481. #else
  482. crc = crc_ccitt(q, 10);
  483. #endif
  484. q[10] = crc >> 8;
  485. q[11] = crc & 0xFF;
  486. }
  487. static char char_to_isrc(char c)
  488. {
  489. if (c >= '0' && c <= '9')
  490. return c - '0';
  491. if (c >= 'A' && c <= 'Z')
  492. return 0x11 + (c - 'A');
  493. if (c >= 'a' && c <= 'z')
  494. return 0x11 + (c - 'a');
  495. /* ts A61008 : obsoleted by test in burn_track_set_isrc() */
  496. /* a ssert(0); */
  497. return 0;
  498. }
  499. void subcode_user(struct burn_write_opts *o, unsigned char *subcodes,
  500. unsigned char tno, unsigned char control,
  501. unsigned char indx, struct isrc *isrc, int psub)
  502. {
  503. struct burn_drive *d = o->drive;
  504. unsigned char *p, *q;
  505. int crc;
  506. int m, s, f, c, qmode; /* 1, 2 or 3 */
  507. memset(subcodes, 0, 96);
  508. p = subcodes;
  509. if ((tno == 1) && (d->rlba == -150))
  510. memset(p, 0xFF, 12);
  511. if (psub)
  512. memset(p, 0xFF, 12);
  513. q = subcodes + 12;
  514. qmode = 1;
  515. /* every 1 in 10 we can do something different */
  516. if (d->rlba % 10 == 0) {
  517. /* each of these can occur 1 in 100 */
  518. if ((d->rlba / 10) % 10 == 0) {
  519. if (o->has_mediacatalog)
  520. qmode = 2;
  521. } else if ((d->rlba / 10) % 10 == 1) {
  522. if (isrc && isrc->has_isrc)
  523. qmode = 3;
  524. }
  525. }
  526. /* ts A61010 : this cannot happen. Assert for fun ? */
  527. /* a ssert(qmode == 1 || qmode == 2 || qmode == 3); */
  528. switch (qmode) {
  529. case 1:
  530. q[1] = dec_to_bcd(tno); /* track number */
  531. q[2] = dec_to_bcd(indx); /* index XXX read this shit
  532. from the track array */
  533. burn_lba_to_msf(d->rlba, &m, &s, &f);
  534. q[3] = dec_to_bcd(m); /* rel min */
  535. q[4] = dec_to_bcd(s); /* rel sec */
  536. q[5] = dec_to_bcd(f); /* rel frame */
  537. q[6] = 0; /* zero */
  538. burn_lba_to_msf(d->alba, &m, &s, &f);
  539. q[7] = dec_to_bcd(m); /* abs min */
  540. q[8] = dec_to_bcd(s); /* abs sec */
  541. q[9] = dec_to_bcd(f); /* abs frame */
  542. break;
  543. case 2:
  544. /* media catalog number */
  545. q[1] = (o->mediacatalog[0] << 4) + o->mediacatalog[1];
  546. q[2] = (o->mediacatalog[2] << 4) + o->mediacatalog[3];
  547. q[3] = (o->mediacatalog[4] << 4) + o->mediacatalog[5];
  548. q[4] = (o->mediacatalog[6] << 4) + o->mediacatalog[7];
  549. q[5] = (o->mediacatalog[8] << 4) + o->mediacatalog[9];
  550. q[6] = (o->mediacatalog[10] << 4) + o->mediacatalog[11];
  551. q[7] = o->mediacatalog[12] << 4;
  552. q[8] = 0;
  553. burn_lba_to_msf(d->alba, &m, &s, &f);
  554. q[9] = dec_to_bcd(f); /* abs frame */
  555. break;
  556. case 3:
  557. c = char_to_isrc(isrc->country[0]);
  558. /* top 6 bits of [1] is the first country code */
  559. q[1] = c << 2;
  560. c = char_to_isrc(isrc->country[1]);
  561. /* bottom 2 bits of [1] is part of the second country code */
  562. q[1] += (c >> 4);
  563. /* top 4 bits if [2] is the rest of the second country code */
  564. q[2] = c << 4;
  565. c = char_to_isrc(isrc->owner[0]);
  566. /* bottom 4 bits of [2] is part of the first owner code */
  567. q[2] += (c >> 2);
  568. /* top 2 bits of [3] is the rest of the first owner code */
  569. q[3] = c << 6;
  570. c = char_to_isrc(isrc->owner[1]);
  571. /* bottom 6 bits of [3] is the entire second owner code */
  572. q[3] += c;
  573. c = char_to_isrc(isrc->owner[2]);
  574. /* top 6 bits of [4] are the third owner code */
  575. q[4] = c << 2;
  576. /* [5] is the year in 2 BCD numbers */
  577. q[5] = dec_to_bcd(isrc->year % 100);
  578. /* [6] is the first 2 digits in the serial */
  579. q[6] = dec_to_bcd(isrc->serial % 100);
  580. /* [7] is the next 2 digits in the serial */
  581. q[7] = dec_to_bcd((isrc->serial / 100) % 100);
  582. /* the top 4 bits of [8] is the last serial digit, the rest is
  583. zeros */
  584. q[8] = dec_to_bcd((isrc->serial / 10000) % 10) << 4;
  585. burn_lba_to_msf(d->alba, &m, &s, &f);
  586. q[9] = dec_to_bcd(f); /* abs frame */
  587. break;
  588. }
  589. q[0] = (control << 4) + qmode;
  590. #ifdef Libburn_no_crc_C
  591. crc = 0; /* dummy */
  592. #else
  593. crc = crc_ccitt(q, 10);
  594. #endif
  595. q[10] = crc >> 8;
  596. q[11] = crc & 0xff;
  597. }
  598. int sector_lout(struct burn_write_opts *o, unsigned char control, int mode)
  599. {
  600. struct burn_drive *d = o->drive;
  601. unsigned char subs[96];
  602. unsigned char *data;
  603. data = get_sector(o, NULL, mode);
  604. if (!data)
  605. return 0;
  606. /* ts A61010 */
  607. if (convert_data(o, NULL, mode, data) <= 0)
  608. return 0;
  609. subcode_lout(o, control, subs);
  610. convert_subs(o, mode, subs, data);
  611. if (sector_headers(o, data, mode, 0) <= 0)
  612. return 0;
  613. sector_common(++)
  614. return 1;
  615. }
  616. int sector_data(struct burn_write_opts *o, struct burn_track *t, int psub)
  617. {
  618. struct burn_drive *d = o->drive;
  619. unsigned char subs[96];
  620. unsigned char *data;
  621. data = get_sector(o, t, t->mode);
  622. if (data == NULL)
  623. return 0;
  624. /* ts A61010 */
  625. if (convert_data(o, t, t->mode, data) <= 0)
  626. return 0;
  627. /* ts A61031 */
  628. if ((t->open_ended || t->end_on_premature_eoi) && t->track_data_done) {
  629. unget_sector(o, t->mode);
  630. return 2;
  631. }
  632. /* ts A61219 : allow track without .entry */
  633. if (t->entry == NULL)
  634. ;
  635. else if (!t->source->read_sub)
  636. subcode_user(o, subs, t->entry->point,
  637. t->entry->control, 1, &t->isrc, psub);
  638. else if (!t->source->read_sub(t->source, subs, 96))
  639. subcode_user(o, subs, t->entry->point,
  640. t->entry->control, 1, &t->isrc, psub);
  641. convert_subs(o, t->mode, subs, data);
  642. if (sector_headers(o, data, t->mode, 0) <= 0)
  643. return 0;
  644. sector_common(++)
  645. return 1;
  646. }
  647. int burn_msf_to_lba(int m, int s, int f)
  648. {
  649. if (m < 90)
  650. return (m * 60 + s) * 75 + f - 150;
  651. else
  652. return (m * 60 + s) * 75 + f - 450150;
  653. }
  654. void burn_lba_to_msf(int lba, int *m, int *s, int *f)
  655. {
  656. if (lba >= -150) {
  657. *m = (lba + 150) / (60 * 75);
  658. *s = (lba + 150 - *m * 60 * 75) / 75;
  659. *f = lba + 150 - *m * 60 * 75 - *s * 75;
  660. } else {
  661. *m = (lba + 450150) / (60 * 75);
  662. *s = (lba + 450150 - *m * 60 * 75) / 75;
  663. *f = lba + 450150 - *m * 60 * 75 - *s * 75;
  664. }
  665. }
  666. int dec_to_bcd(int d)
  667. {
  668. int top, bottom;
  669. top = d / 10;
  670. bottom = d - (top * 10);
  671. return (top << 4) + bottom;
  672. }
  673. int sector_headers_is_ok(struct burn_write_opts *o, int mode)
  674. {
  675. if (mode & BURN_AUDIO) /* no headers for "audio" */
  676. return 1;
  677. if (o->write_type == BURN_WRITE_SAO)
  678. return 1;
  679. /* ts A61031 */
  680. if (o->write_type == BURN_WRITE_TAO)
  681. return 1;
  682. if (mode & BURN_MODE1)
  683. return 2;
  684. return 0;
  685. }
  686. /* ts A90830 : changed return type to int
  687. @return 0= failure
  688. 1= success
  689. */
  690. int sector_headers(struct burn_write_opts *o, unsigned char *out,
  691. int mode, int leadin)
  692. {
  693. #ifdef Libburn_ecma130ab_includeD
  694. struct burn_drive *d = o->drive;
  695. unsigned int crc;
  696. int min, sec, frame;
  697. int modebyte = -1;
  698. int ret;
  699. ret = sector_headers_is_ok(o, mode);
  700. if (ret != 2)
  701. return !!ret;
  702. modebyte = 1;
  703. out[0] = 0;
  704. memset(out + 1, 0xFF, 10); /* sync */
  705. out[11] = 0;
  706. if (leadin) {
  707. burn_lba_to_msf(d->rlba, &min, &sec, &frame);
  708. out[12] = dec_to_bcd(min) + 0xA0;
  709. out[13] = dec_to_bcd(sec);
  710. out[14] = dec_to_bcd(frame);
  711. out[15] = modebyte;
  712. } else {
  713. burn_lba_to_msf(d->alba, &min, &sec, &frame);
  714. out[12] = dec_to_bcd(min);
  715. out[13] = dec_to_bcd(sec);
  716. out[14] = dec_to_bcd(frame);
  717. out[15] = modebyte;
  718. }
  719. if (mode & BURN_MODE1) {
  720. #ifdef Libburn_no_crc_C
  721. crc = 0; /* dummy */
  722. #else
  723. crc = crc_32(out, 2064);
  724. #endif
  725. out[2064] = crc & 0xFF;
  726. crc >>= 8;
  727. out[2065] = crc & 0xFF;
  728. crc >>= 8;
  729. out[2066] = crc & 0xFF;
  730. crc >>= 8;
  731. out[2067] = crc & 0xFF;
  732. }
  733. if (mode & BURN_MODE1) {
  734. memset(out + 2068, 0, 8);
  735. burn_rspc_parity_p(out);
  736. burn_rspc_parity_q(out);
  737. }
  738. burn_ecma130_scramble(out);
  739. return 1;
  740. #else /* Libburn_ecma130ab_includeD */
  741. int ret;
  742. ret = sector_headers_is_ok(o, mode);
  743. if (ret != 2)
  744. return (!! ret);
  745. /* ts A90830 : lec.c is copied from cdrdao.
  746. I have no idea yet how lec.c implements the Reed-Solomon encoding
  747. which is described in ECMA-130 for CD-ROM.
  748. So this got removed for now.
  749. */
  750. libdax_msgs_submit(libdax_messenger, o->drive->global_index,
  751. 0x0002010a,
  752. LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
  753. "Raw CD write modes are not supported", 0, 0);
  754. return 0;
  755. #endif /* ! Libburn_ecma130ab_includeD */
  756. }
  757. #if 0
  758. void process_q(struct burn_drive *d, unsigned char *q)
  759. {
  760. unsigned char i[5];
  761. int mode;
  762. mode = q[0] & 0xF;
  763. /* burn_print(12, "mode: %d : ", mode);*/
  764. switch (mode) {
  765. case 1:
  766. /* burn_print(12, "tno = %d : ", q[1]);
  767. burn_print(12, "index = %d\n", q[2]);
  768. */
  769. /* q[1] is the track number (starting at 1) q[2] is the index
  770. number (starting at 0) */
  771. #warning this is totally bogus
  772. if (q[1] - 1 > 99)
  773. break;
  774. if (q[2] > d->toc->track[q[1] - 1].indices) {
  775. burn_print(12, "new index at %d\n", d->alba);
  776. d->toc->track[q[1] - 1].index[q[2]] = d->alba;
  777. d->toc->track[q[1] - 1].indices++;
  778. }
  779. break;
  780. case 2:
  781. /* XXX do not ignore these */
  782. break;
  783. case 3:
  784. /* burn_print(12, "ISRC data in mode 3 q\n");*/
  785. i[0] = isrc[(q[1] << 2) >> 2];
  786. /* burn_print(12, "0x%x 0x%x 0x%x 0x%x 0x%x\n", q[1], q[2], q[3], q[4], q[5]);
  787. burn_print(12, "ISRC - %c%c%c%c%c\n", i[0], i[1], i[2], i[3], i[4]);
  788. */
  789. break;
  790. default:
  791. /* ts A61009 : if reactivated then witout Assert */
  792. a ssert(0);
  793. }
  794. }
  795. #endif
  796. /* this needs more info. subs in the data? control/adr? */
  797. /* ts A61119 : One should not use unofficial compiler extensions.
  798. >>> Some day this function needs to be implemented. At least for now
  799. the result does not match the "mode" of cdrecord -toc.
  800. */
  801. /*
  802. #warning sector_identify needs to be written
  803. */
  804. int sector_identify(unsigned char *data)
  805. {
  806. /*
  807. burn_ecma130_scramble(data);
  808. check mode byte for 1 or 2
  809. test parity to see if it's a valid sector
  810. if invalid, return BURN_MODE_AUDIO;
  811. else return mode byte (what about mode 2 formless? heh)
  812. */
  813. return BURN_MODE1;
  814. }