Classic Computer Magazine Archive COMPUTE! ISSUE 94 / MARCH 1988 / PAGE 61

ST Outlook

Philip I. Nelson

Snooping Into Your Disks

Admit it: Most of us treat computer disks like a magician's silk top hat. We toss a chunk of information into the hat, trusting that it will reappear when needed. When that time comes, we perform the right incantation, and—presto—changeo! The same chunk of information recombobulates itself as if by magic.

If it didn't happen so often—and so reliably—this process might seem quite remarkable. We create collections of information called files. A file can be anything from the text of this column, to a dazzling arcade game, to a digitized image of your Uncle Charlie whittling on his front porch. Those files are stored as electromagnetic patterns of ones and zeros on circular sheets of plastic. By reading those patterns from a whirling disk, the computer recreates the file in its original form.

The Veil Removed

This month's program lets you pull aside the veil and peer directly into a disk's individual sectors. It won't make you a GEMDOS expert overnight, but with this program and a decent reference book, you'll have the basic tools for learning a lot about how ST disk files are put together.

By the way, if you're queasy about tampering with disk files, you can be reassured that this program poses absolutely no danger. It can read exactly what's on any sector of a disk, but it can't change what's contained there. So you can use it without risking the loss of valuable information.

The program, DISKPEEK.BAS, is written in GFA BASIC with lots of comments to make it easy to translate into other languages. When you run the program, it prompts you to enter the track and sector (see below) you want to examine. If you enter an illegal value, the program puts up a warning and invites you to try again. A legal entry is rewarded by a sector display similar to the accompanying figure. Since a disk sector contains too many bytes to display on the screen at once, the program shows half of a sector at a time, letting you flip between the first and second halves by pressing a key. Press the Escape key to view a different sector.

Typical Sector Display

Track: 2 Sector: 1 Bytes 0-255

0000:   00 02 47 66 41 42 41 53 49 43 00 00 00 00 00 00  ..GFABASIC......
0010:   00 52 00 00 00 70 00 00 00 70 00 00 00 70 00 00  .R...p...p...p..
0020:   00 70 00 00 00 70 00 00 00 70 00 00 00 70 00 00  .p...p...p...p..
0030:   00 8E 00 00 00 8E 00 00 00 8E 00 00 00 8E 00 00  ,...............
0040:   00 8E 00 00 03 B8 00 00 07 EA 00 00 08 3E 00 00  .............>..
0050:   08 68 00 00 08 68 00 00 08 68 00 00 08 68 00 00  .h...h...h...h..
0060:   08 68 00 00 08 68 00 00 08 68 00 00 08 74 00 00  .h...h...h...t..
0070:   08 74 00 00 08 74 00 00 08 74 00 00 08 74 06 46  .t...t...t...t.F
0080:   4C 4F 50 52 44 06 44 52 56 4E 55 4D 07 53 49 44  LOPRD.DRVNUM.STD
0090:   45 4E 55 4D 08 4E 55 4D 53 45 43 54 53 03 44 55  ENUM.NUMSECTS.DU
00A0:   4D 06 54 52 4B 4E 55 4D 06 53 45 43 4E 55 4D 06  M.TRKNUM.SECNUM.
00B0:   52 45 53 55 4C 54 05 44 55 4D 4D 59 05 53 54 41  RESULT.DUMMY.STA
00C0:   52 54 06 4F 46 46 53 45 54 01 4A 01 4B 01 58 00  RT.OFFSET.J.K.X.
00D0:   04 42 4C 4F 4B 04 41 4C 52 54 06 4F 46 46 53 45  .BLOK.ALRT.OFFSE
00E0:   54 03 41 53 4B 03 42 59 54 01 58 02 4B 59 0A 4E  T.ASK.BYT.X.KY.N
00F0:   45 57 5F 53 45 43 54 4F 52 08 53 48 4F 5F 4C 4F  EW_SECTOR.SHO_LO
        Press   SPACE  for bytes    0-255 of this sector
                RETURN for bytes 256-512 of this sector
                ESC    for a different tract and sector
                CTRL-SHIFT-ALT to quit

The Basic Scheme

Knowing something about disk organization will help you understand what this program shows you. ST disks, like all others, are organized in circular, concentric tracks. There are 80 tracks in all, numbered 0–79. Each track is divided into nine 512-byte sectors, which are numbered 1–9. If you multiply these numbers (80 * 512 * 9) you come up with 368,640 (360K) bytes, the total amount of space available on a single-sided ST disk. A double-sided disk has information on both sides of the disk, and stores twice as much: 737,280 bytes, or exactly 720K. The space available for your use is somewhat smaller, since GEMDOS sets aside small areas on each disk to keep track of your files.

What you see on a given disk depends on what you've stored there. A 1ST Word document file, for instance, contains formatting characters in addition to ordinary text. And a BASIC program looks very different on disk than it does when listed on the screen. The figure shows the very beginning of DISKPEEK.BAS as it is stored in Track 2, Sector 1 on a disk of mine. (On a different disk, of course, the same file might appear on a different track and sector.)

If you compare the figure to the program listing, you'll notice that the program's variable names appear in a bunch near the beginning of the file. With DISKPEEK .BAS, we can discover, at a glance, that GFA BASIC puts a variable table in its .BAS files, and we can examine that table in detail.

Where It All Begins

One place where every devoted disk snooper goes again and again is the directory, which begins at Track 1, Sector 3, Side 0. Each file on the disk has a 32-byte directory entry, which records its name and other information shown in the Disk Directory Entry table.

ST Disk Directory Entry

Byte number Contents
0-10 Name and extension
11 Attribute
12-21 Reserved for future use
22-23 Time of creation
24-25 Date of creation
26-27 Beginning cluster
28-31 Size

In addition to existing files, the directory shows files that have been deleted. With a little experimentation, you ought to be able to deduce how GEMDOS marks a deleted file. (Hint: Look at the first character of the filename.)

Disk storage is a large topic, one that can easily fill a book. If you get interested in the subject, take a look at ST Disk Drives: Inside and Out, published by Abacus Books. I can't recommend this book wholeheartedly, since it does have its share of flaws. Nevertheless, it contains lots of information that's not easily available from any other source.

Disk Peek

For instructions on entering this program, please refer to "COMPUTE!'s Guide to Typing In Programs" elsewhere in this issue.

' DISKPEEK.BAS Examine ST disk sectors
'
floprd = 8      		! XBIOS routine 8 (Floprd) reads a disk sector
drvnum = 0   			! Drive 0 = A:, 1 = B:4
sidenum =0   			! Double-sided disks have sides 0 and 1
numsects =1    			! Number of sectors to read with each Floprd call
dum=0         			! Dummy parameter for Floprd call
1
'
new_sector:			!Read the specified track and sector
CLS
blok$=STRING$(512, 0)                	! 512-byte space for sector contents
INPUT "Track number  (0-79)" trknum	! Track we want to read
INPUT "Sector number (1- 9)";secnum	! Sector we want to read
' Flag illegal track/sector choice
IF trknum<0 OR trknum>79 OR secnum<1 OR secnum >9 THEN
  alrt$="Illegal track ("+STR$(trknum)+")
  !or sector ("+STR$(secnum)+")"
  ALERT 1, alrt$, 1, "Oops", dum"
  GOTO new_sector
ENDIF
' Call the XBIOS routine Floprd to read a disk sector.
result = XBIOS(f1oprd, L : VARPTR(blok$), L : dum, drvnum, secnum, trknum, sidenum, numsects)
' Check for a disk error. Return code of zero means no error occurred.
IF result<>0 THEN
ALERT 1, "Disk error!!Gotta go.", 1, "OK",dummy
END
ENDIF
start = 0
'
sho_loop:  					! Display sector contents CLS
PRINT SPACE$(10);"Track: ";trknum;SPACE$(5);"Sector: " : "secnum;
PRINT SPACE$(5);"Bytes ";start;"-";start+255 offset = start
PRINT
FOR j=0 TO 15
  offset$ = HEX$(offset)
  WHILE LEN(offset$)>4
    offset$ = "0"+offset$ WEND
  PRINT offset=offset + 16
  ask$=""
  byt$=""
  FOR k = 0 TO 15
    x = PEEK(VARPTR(blok$)+start+(16*j)+k) byt$=byt$+CHR$(x)
    IF x<16 THEN
      ask$;=ask$+"0"
    ENDIF
    ask=ask+HEX(x)+" "
   NEXT k
   PRINT ask$;
   FOR K=1 TO 16
     x$ = MID$(byt$, k, 1)
     IF x$<CHR$(33) OR x$>"z"  THEN
       PRINT ".";
     ELSE
       PRINT x$;
     ENDIF
   NEXT k
   PRINT
NEXT j
PRINT
PRINT "Press SPACE  for bytes   0-255 of this sector"
PRINT "      RETURN for bytes 256-512 of this sector"
PRINT "	ESC for a different track and sector"
PRINT "  CTRL-SHIFT-ALT to quit"
get_a_key:
ky$=""
WHILE ky$=""	! Wait for a keypress.
   ky$ = INKEY$
WEND
IF ky$=CHR$(27) THEN		! You pressing ESC.
GOTO  new_sector		! Choose new track/sector.
ENDIF
IF ky$=CHR$(13) AND start=0 THEN start = 256 		! View bytes 256-511 of this sector
GOTO sho_loop
ENDIF
IF ky$=CHR$(32) AND start=256 THEN start=0	! View bytes 0-255 of this
        sector GOTO sho_loop
ENDIF
GOTO get_a_key