Solved

cd TOC access

Posted on 1997-06-19
5
274 Views
Last Modified: 2013-12-26
I have a SCSI CD-Rom drive with ID=3.  I am trying to access the TOC on the drive and continually get and error of -21 (badUnitErr).  I have downloaded samples from Apple, but i cannot get those to work either.  
I am using the PBControl call (this returns with -21) but fill in the paramblock with what i beleive are the correct values.

If you could send me some code that reads off theTOC from the disk (using the mode 4 which gives the starting address of all tracks), it would be very helpful.

thanks
kevin
0
Comment
Question by:meyerkl
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
Paul B earned 100 total points
Comment Utility
Here is a short program I wrote to show how to do this.
Apples example is screwed up.
If this does not work please tell me whos driver software you are using. It could be a driver bug!

The code is heavily commented
Notes:
You MUST know the drive number of the CD!
The drive MUST support mode 4 Read Toc
The CD MUST be in the drive

Paul Baxter
pbaxter@assistivetech.com

#include <string.h>
#include <stdio.h>

#define kREADTOC            100                              // Read Toc command
#define kGETALL                  4                              // mode of read toc
#define kWHOISTHERE            97                              // test to see if CD drive

#define kBADRESULT            0xDEAD                        // error code
#define kBUFSIZE            512                              // buffer size to read TOC into


/* Prototypes */
void ReadTocALL(void);
OSErr ReadToc(short driveNum, Ptr buffer);
Boolean IsCDDrive(short driveNum);
short GetDriverRefNum(short driveNum);
void ReadTocALL(void);

char gBuffer[kBUFSIZE];

void main(void)
{
      ReadTocALL();
}

// Read TOC for ALL CD Drives
void ReadTocALL(void)
{
       QHdrPtr                  drvQHdr;
      DrvQEl                  *CurrDrvPtr;
      short                  driveNum, count;
      OSErr                  err;

      count = 0;                                                // number of CD drives found
       drvQHdr = GetDrvQHdr();                              // get the drive que head
      CurrDrvPtr = (DrvQEl *)drvQHdr->qHead;      // set current entry
      while (CurrDrvPtr) {                              // loop until out of range
            driveNum = CurrDrvPtr->dQDrive;            // get drive number
                                                                  // Is this a CD drive
            if (IsCDDrive(driveNum)) {
                  count++;                                    // count number of CD drives
                                                                  // Read TOC for this drive
                  err = ReadToc(driveNum, gBuffer);
                  if (!err) {
                        // Success!!!!!!!!!!!!!!!!!!
                        // TOC data is in gBuffer
                  }
                  printf("drive %d Read TOC  result %d\n", driveNum, err);
            }

            CurrDrvPtr = (DrvQEl *)CurrDrvPtr->qLink;
      }
      if (count == 0)      printf("NO CD drive found!!\nMake sure CD is in drive\n");
}


// Read TOC mode 4 into a buffer
// buffer MUST be pre allocated
// driveNum == drive number of the CD (not SCSI ID)
OSErr ReadToc(short driveNum, Ptr buffer)
{
       CntrlParam ctlPB;                                    // param block
       OSErr      err;                                          // result
       
      memset(&ctlPB, 0, sizeof(CntrlParam));      // Clear param block
                                                                  // get the driver
      ctlPB.ioCRefNum = GetDriverRefNum(driveNum);
                                                                  // check for error
      if (ctlPB.ioCRefNum == kBADRESULT)      return badUnitErr;
      ctlPB.csCode = kREADTOC;                        // set Read TOC Command
       ctlPB.ioVRefNum = driveNum;                        // Drive number (NOT volume number)
       ctlPB.csParam[0] = kGETALL;                        // set mode
                                                                   // set buffer
      ctlPB.csParam[1] = HiWord((long)buffer);
      ctlPB.csParam[2] = LoWord((long)buffer);
                                                                  // call the driver
      err = PBControlSync((ParmBlkPtr) &ctlPB);
      if (err == noErr) err = ctlPB.ioResult;      // check ioResult field
      return err;                                                // return the result
}

// get the driver refnum given a drive number
short GetDriverRefNum(short driveNum)
{
       QHdrPtr                  drvQHdr;
      DrvQEl                  *CurrDrvPtr;

       drvQHdr = GetDrvQHdr();                              // get the drive que head
      CurrDrvPtr = (DrvQEl *)drvQHdr->qHead;      // set current entry
      while (CurrDrvPtr) {                              // loop until out of range
                                                                  // is this the right drive?
            if (CurrDrvPtr->dQDrive == driveNum) {      
                  return(CurrDrvPtr->dQRefNum);      // set return value
            }
            else {                                                // goto next element
                  CurrDrvPtr = (DrvQEl *)CurrDrvPtr->qLink;
            }
      }
      return kBADRESULT;                                    // return
}

// return true if the drive is a CD drive
Boolean IsCDDrive(short driveNum)
{
      short refnum;
      
       CntrlParam ctlPB;                                    // param block
       OSErr      err;                                          // result
       
      memset(&ctlPB, 0, sizeof(CntrlParam));      // Clear param block
                                                                  // get the driver
      ctlPB.ioCRefNum = GetDriverRefNum(driveNum);
                                                                  // check for error
      if (ctlPB.ioCRefNum == kBADRESULT)      return false;
      
      ctlPB.csCode = kWHOISTHERE;                        // id the a CD?
      err = PBStatusSync((ParmBlkPtr) &ctlPB);// call the drivr
      if (err == noErr) err = ctlPB.ioResult;      // check ioResult field
      return err == noErr;
}

0
 

Author Comment

by:meyerkl
Comment Utility
just a quick comment:
  I also want to be able to read the Q-subcode on different tracks.  I know that there is a different value you place in csCode but i was wondering if i could just use your functions to place the call with the different value.  ie: I know that Apple has some samples that do not work, just wondering how general your code is...

thanks alot!!!!!
0
 
LVL 1

Expert Comment

by:Paul B
Comment Utility
To ReadQ it is pretty much the same once you know the drive number.
ReadQ will return the data in csParams 0..8 so you should copy the data to somewhere else

This function will copy the data to the buffer passed

#define kREADQ                        101
#define kREADQSIZE 9

OSErr ReadQ(short driveNum, Ptr buffer)
{
       CntrlParam ctlPB;                                    // param block
       OSErr      err;                                                // result
       
      memset(&ctlPB, 0, sizeof(CntrlParam));      // Clear param block
                                                                  // get the driver
      ctlPB.ioCRefNum = GetDriverRefNum(driveNum);
                                                                  // check for error
      if (ctlPB.ioCRefNum == kBADRESULT)      return badUnitErr;
      ctlPB.csCode = kREADQ;                              // set Read TOC Command
       ctlPB.ioVRefNum = driveNum;                        // Drive number (NOT volume number)
       err = PBControlSync((ParmBlkPtr) &ctlPB);
      if (err == noErr) err = ctlPB.ioResult;      // check ioResult field
 if (err == noErr) {
          BlockMovedata(&ctlPB.csParam[0], buffer, kREADQSIZE);
 }
      return err;                                                // return the result
}
0
 
LVL 1

Expert Comment

by:Paul B
Comment Utility
ooops...BlockMovedata should be BlockMoveData :/


0
 

Author Comment

by:meyerkl
Comment Utility
Paul,
      I noticed on your Customer Infomation page here that you have written some software for CD-ROM mastering.  I am trying to write something like that but for my own use.  I was wondering if you have an e-mail address I could ask you questions and stuff.

thanks.
meyerkl@crux.rose-hulman.edu
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now