TweetFollow Us on Twitter

Disks
Volume Number:1
Issue Number:5
Column Tag:FORTH FORUM

Disks

By Jörg Langowski

This month we are going to look at the organization of the 400 K bytes on the standard Macintosh disk. The operating system does a very good job of hiding this organization from you, but for patching disks, changing file attributes, and looking at files of unknown structure it is very convenient to know a little more about the ‘deep structure’ of the Macintosh disk.

Fortunately, it is very easy to read any byte at any position on the disk. The toolbox routines READ and WRITE do not make any distinction between files and whole disks. Let’s recall how a read or write through a toolbox call is done. The toolbox traps are A002 for read and A003 for write. You have to set up a file control block, pass its address in register A0, and execute the trap. File I/O, direct disk I/O, serial I/O and even the sound generation are all handled through this mechanism. The only difference is in the file control block (FCB). It has the following structure:

Bytes 0 11 : Header; IM tells us nothing about it

Bytes 12 15 : Address of the I/O completion routine

Bytes 16 17 : I/O result code ( also returned in D0 )

Bytes 18 21 : Pointer to filename string ( for files )

Bytes 22 23 : Drive number ( for direct disk I/O )

Bytes 24 25 : Reference number ( explained later )

Bytes 32 35 : Pointer to data buffer

Bytes 36 39 : Requested byte count for I/O operation

Bytes 40 43 : Actual # of bytes read/written

Bytes 44 45 : Positioning mode, 0 : relative, 1 : absolute

Bytes 46 49 : Position offset ( in bytes )

If you want to do file I/O, you have to open the file first. This is done by setting up the FCB with a valid file name (with optional volume prefix) and calling the trap OPEN ( A000 ). This will return a reference number (positive 16-bit integer) in the FCB, through which all read/write calls are made from now on.

The only difference between doing a file I/O and direct device I/O operation is this reference number. The predefined Macintosh device drivers have negative reference numbers. They are listed on pages 22 and 23 of the Device Manager Programmer’s Guide in IM, and the important one for us is the disk reference number, -5. If you set up the FCB like above, with the reference number -5, and then do a READ or WRITE call, the disk will be read/written directly. That is, the operating system treats the whole disk as one large file 400K bytes long. The position from which I/O starts is given by the offset in bytes 46 49, and the number of bytes to I/O is in 36 39. If 44 45 contain a 0, the offset is counted from the last byte read/written; if it is one, is is counted from the start of the disk. After the I/O is completed, a result code will be returned in the FCB and in register D0. Zero means that everything went OK; a negative return code means that something was wrong. For instance, if you try to read or write to a non-existing position on the disk, -67 is returned; -50 is returned if the number of bytes actually read into the buffer is greater than the number requested. This happens if you don’t read an integer multiple of 512 bytes; the number is then rounded up to the next 512 bytes.

In FORTH we call the traps through the defining word OS.TRAP. The FCB address is then passed on the stack, and the result code is stored into the variable IO-RESULT.

This is about all the information you need to understand the simple disk editor program that is listed at the end. It is menu-oriented and reads, writes, dumps to screen and modifies any 1024 byte block on the (internal) disk. Using that program, you can verify very easily what I am telling you in the rest of this article.

Macintosh disks are read and written by the operating system in 512 byte blocks (‘logical blocks’). However, the operating system refers to 1K blocks as the smallest unit (‘allocation block’). Therefore, the program reads 2 logical blocks at a time and the block number that the program asks for is INT(logical block number / 2).

A Directory Entry

With the described program it is now quite easy to figure out some facts about Mac disk organization; the IM manual helps, too. Starting with block O as the first block on the disk, the directory resides in logical blocks $4 to $B (allocation blocks $2 to $5); easily recognizable because all the file names are there. The map in Fig. 1 shows the structure of a directory entry.

The first part of the entry tells the system several parameters it needs to know about the file. ‘Attributes’ contains 8 bits of file attributes. For instance, Bit 7 set means that the file is open, bit 0 set means it is software locked, . Bit 6 is the copy protect bit. If you reset this one to zero, you will be able to copy a ‘protected’ file by dragging the icon. Bytes $2-$5 give the file type, such as APPL (application), ZSYS (system file) or TEXT (text file) in ASCII format, bytes $6-$9 give the creator. The four Finder words contain information that is used by the Finder internally. All directory entries are numbered sequentially ($14-$15).

Bytes $16 and $17 (16 bit integer) give the starting block of the data fork, bytes $18 to $1B (32 bit integer) its length in bytes and bytes $1C to $1F this length rounded up to the next 512 byte boundary. (The blocks referred to in the directory entry are allocation blocks; block number 2 starts right after the last directory block). The resource fork is referenced in the same way by the next 10 bytes in the directory entry. The creation and modification dates of the file are kept in the next 8 bytes.

The last part of the entry gives the file name; remarkable here is that the directory entries are not all the same length. Since file names may be up to 255 characters long, reserving the maximum space for every file name would be inefficient; therefore the name is stored as a standard string starting at byte $32 (Hex) with a length byte and the name thereafter.

The volume information table

Logical blocks $2 and $3 (Fig. 2) on the Macintosh disk contain information about the disk itself and a block allocation table that tells the system which blocks are in use.

The first two bytes are always $D2D7; if they are not, the disk will not be recognized as a Macintosh disk. Following that are two 4-byte words that give the time and date of initialization and last backup. The 16-bit word Volume Attributes will have bit 7 set if the write protect latch is set on the diskette and bit 15 set if the disk is locked by software. The volume copy protection bit is also located here, it is bit 14 and if you reset it, the disk will be copyable with the Disk Copy routine on the System Disk, regardless of whether individual files are ‘protected’ or not. The next entries give the total number of files in the directory, the first logical block of the file directory and the number of logical blocks in the directory.

Following are the total number of allocation blocks on the volume and the size of the allocation block in bytes ($0400 on a standard Macintosh disk). The meaning of the remaining parameters should be clear from the diagram.

IM describes how the volume allocation block map is organized; I’ll quickly repeat that here. Every allocation block (1024 bytes) is represented by a 12-bit entry. If this entry is zero, the block is unused. If it is used in a file, it contains the number of the next block in the file. The last block in the file is indicated by a 1.

: disk.editor ;
18 field +fcb.name    22 field +fcb.drive   24 field +fcb.vrefnum 
32 field +fcb.buf     36 field +fcb.request 40 field +fcb.actual
44 field +fcb.posmode 46 field +fcb.position
12 constant dsk.menu

variable vol.fcb  variable vol.fnumber  variable hex.asc
create this.fcb 50 allot     create vol.buffer 1024 allot
hex a002 os.trap read  a003 os.trap write decimal

: open.vol  this.fcb dup vol.fcb ! dup +fcb.vrefnum -5 swap w!
            +fcb.drive 1 swap w!  ;

: input 0 0 >in ! query 32 word convert drop ;

: dump.fcb .” Header    : “ 3 0 do dup i 4* + @ . .”  “ loop cr
   .” completion: “ dup 12 + @ . cr  .” ioresult  : “ dup 16 + w@ . cr
   .” filename  : “ dup 18 + @ . cr  .” drive     : “ dup 22 + w@ . cr
   .” refnum    : “ dup 24 + w@ . cr .” buffer    : “ dup 32 + @ . cr
   .” request   : “ dup 36 + @ . cr  .” actual    : “ dup 40 + @ . cr
   .” posmode   : “ dup 44 + w@ . cr .” offset    : “ dup 46 + @ . cr 
;

: setup.fcb ( buffer \ block# \ fcb -- fcb )
 dup +fcb.posmode 1 swap w!  dup +fcb.position rot 1024 * swap !
 dup +fcb.buf rot swap !     dup +fcb.request 1024 swap ! ;

: read.pb ( buffer \ block# \ fcb -- )    setup.fcb  read ;
: read.disk ( block# -- ) vol.buffer swap vol.fcb @ read.pb ;

: write.pb ( buffer \ block# \ fcb -- )    setup.fcb  write ;
: write.disk ( block# -- )  vol.buffer swap vol.fcb @ write.pb ;

: dump.32 ( start address -- )
  32 0 do dup i + c@ hex.asc @ if
                   dup 16 < if .” 0" then . else
                   dup 32 < if .” .” drop else emit then then loop ;

: dump.buffer ( buffer address -- )
 9 textsize 9 line.height condensed textstyle cr
 32 0 do dup i 32 * dup 16 < if .” 00" else dup 256 < if .” 0" then then
         dup . +  dump.32 drop cr loop ;

: read.block 12 textsize 15 line.height plain textstyle cr
   .” Read block #: “ input dup 0< if error” Negative Block #” then
       read.disk io-result @ 0= not if cr .” OS Error “ io-result @ . 
cr abort
                                     else .” block read” cr  then ;

: write.block 12 textsize 15 line.height plain textstyle cr
   .” Write to block #: “ input dup 0< if error” Negative Block #” then
     write.disk io-result @ 0= not if cr .” OS Error “ io-result @ . 
cr abort
                                     else .” block written” cr then ;

: dump.block hex vol.buffer dump.buffer decimal ;

: patch.block 12 textsize 15 line.height plain textstyle cr
  .” change byte#: “ hex input decimal dup 1023 >
  if .” too large” cr abort then
             vol.buffer + .” to: “ hex input decimal swap c! ;

: set.hex 1 hex.asc ! 6 -1 dsk.menu item.check  7 0 dsk.menu item.check 
;

: set.ascii 0 hex.asc !  6 0 dsk.menu item.check  7 -1 dsk.menu item.check 
;

: disk.menu
   0 “ DiskEdit” dsk.menu new.menu
    “ Read;Write;Dump;Change;-(;Hex;Ascii” dsk.menu append.items
     draw.menu.bar dsk.menu menu.selection:
     0 hilite.menu   case
     1 of  read.block      endof  2 of  write.block     endof
     3 of  dump.block      endof  4 of  patch.block     endof
     6 of  set.hex         endof  7 of  set.ascii       endof
                     endcase
     events on do.events abort ;
  
disk.menu set.hex open.vol

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

iClock 4.6.4 - Customizable menu bar clo...
iClock replaces the old Apple's default menu bar clock with more features, customization and increases your productivity. Features: Have your Apple or Google calendar instantly available from the... Read more
Final Cut Pro X 10.4.5 - Professional vi...
Final Cut Pro X is a professional video editing solution. Completely redesigned from the ground up, Final Cut Pro adds extraordinary speed, quality, and flexibility to every part of the post-... Read more
MarsEdit 4.2.6 - Quick and convenient bl...
MarsEdit is a blog editor for OS X that makes editing your blog like writing email, with spell-checking, drafts, multiple windows, and even AppleScript support. It works with with most blog services... Read more
Microsoft Office 2016 16.16.6 - Popular...
Microsoft Office 2016 - Unmistakably Office, designed for Mac. The new versions of Word, Excel, PowerPoint, Outlook, and OneNote provide the best of both worlds for Mac users - the familiar Office... Read more
calibre 3.38.0 - Complete e-book library...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more
Microsoft Office 365, 2019 16.21 - Popul...
Microsoft Office 365. The essentials to get it all done. Unmistakably Office, designed for Mac Get started quickly with new, modern versions of Word, Excel, PowerPoint, Outlook and OneNote-... Read more
Microsoft Office 365, 2019 16.21 - Popul...
Microsoft Office 365. The essentials to get it all done. Unmistakably Office, designed for Mac Get started quickly with new, modern versions of Word, Excel, PowerPoint, Outlook and OneNote-... Read more
Microsoft Office 2016 16.16.6 - Popular...
Microsoft Office 2016 - Unmistakably Office, designed for Mac. The new versions of Word, Excel, PowerPoint, Outlook, and OneNote provide the best of both worlds for Mac users - the familiar Office... Read more
Final Cut Pro X 10.4.5 - Professional vi...
Final Cut Pro X is a professional video editing solution. Completely redesigned from the ground up, Final Cut Pro adds extraordinary speed, quality, and flexibility to every part of the post-... Read more
calibre 3.38.0 - Complete e-book library...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more

Latest Forum Discussions

See All

Pre-register now for epic fantasy RPG Fi...
Final Blade is an epic fantasy RPG that revolves around two warring nations and one extremely powerful sword. Having already found great success in Taiwan and South Korea, the developer is now bringing its acclaimed free-to-play iOS and Android... | Read more »
The top 3 best iPhone and iPad games com...
The end of the week has rolled around. It's been a pretty good seven days for mobile gaming - if you want proof you can click here and check out our five favorite games that came out this week - but now we're going to turn our attention to the... | Read more »
The best games for iPhone and iPad that...
Every week here at 148Apps we like to make sure that you're up to date with all the awesome games that have come out for iPhone and iPad over the past 7 days. We pick the cream of the crop to ensure that you don't have to play anything boring and... | Read more »
The top 3 best idle games for iPhone and...
Sometimes you want to play a game where you don't really have to do anything. Don't worry, we're not here to judge you, we're exactly the same. We love some action when we're in the right mood, but sometimes we want something a little more chill -... | Read more »
The top 5 best games with great stories...
Last week we took a look at the best games for iOS that you could waste hours and hours of your life playing, so now we thought we'd have a look at the top 5 games for iPhone and iPad that have great stories. These are the tales that have stuck... | Read more »
Langrisser Mobile preview - Seminal TRPG...
Langrisser Mobile is set to finally break the revolutionary TRPG franchise's 20 year silence when the it finally makes its way to touchscreens the world over at the end of the month. The original Langrisser game (known as Warsong in the west)... | Read more »
Everything you need to win at Dragalia L...
Want to up your Dragalia Lost game? You've come to the right place. Here at 148Apps, we've been digging deep into Nintendo's latest gacha game to find the best tips, tricks, and strategies to share with all of our future co-op team mates (read: you... | Read more »
How will World of Warcraft, Starcraft, a...
You might have heard that Blizzard is bringing Diablo to mobile with Diablo Ultimate. There's been plenty of controversy surrounding that decision, and it's meant that a really important piece of information has been glossed over a little. Namely... | Read more »
The top 3 best iPhone and iPad games com...
Our mission here at 148Apps is pretty simple - we want to make sure that you're playing the best iPhone and iPad games in the world. Which is why, every Friday, we like to give you an insight into the incredible games that are set to rock your... | Read more »
The top 5 best games for iPhone and iPad...
It's been a pretty decent week for releases on the App Store, all told. This week has the feeling of a mobile-first start to the year, with developers releasing games that fit perfectly in the palm of your hand. And we've picked what we think are... | Read more »

Price Scanner via MacPrices.net

New US Cellular iPhone deal: Switch and get a...
US Cellular has introduced a new deal offering free or discounted Apple iPhones to new customers. Switch to US Cellular and get a 64GB iPhone Xr for free or get a 64GB iPhone Xs or Xs Max for 50% off... Read more
20% off iPhones with plan at Total Wireless w...
Use coupon code TWNEWYEAR20 at Total Wireless from January 5, 2019 to January 31, 2019, and take 20% off their selection of new iPhones or other devices. Total Wireless plan is required, and the... Read more
Price drop! B&H now offering all new 27″...
B&H Photo has lowered prices on new 27″ Apple iMacs, and they’re now offering each model for $200 off MSRP, starting at $1599. These are the same models offered by Apple in their retail and... Read more
B&H has Apple Watch Series 3 GPS + Cellul...
B&H Photo is discounting Apple Watch Series 3 GPS + Cellular models by $60-$70, with 38mm Watches starting at $309. Shipping is free: – 38mm Apple Watch Series 3 GPS + Cellular: $309 $70 off MSRP... Read more
Get a 32GB iPhone 6s at Boost Mobile for $299...
Boost Mobile is offering the 32GB Apple iPhone 6s and 6s Plus for $150 off MSRP plus a free $100 account credit. Their discount reduces the cost of a 32GB iPhone 6s to $299.99 and the 32GB iPhone 6s... Read more
Get a new 9.7″ Apple iPad for $75 off at Veri...
Use coupon code TABLET75 at checkout to save $75 on a 9.7″ iPad at Verizon for a limited time. This reduces the price for the 32GB model from $459 to $384 and the 128GB iPad from $559 to $484. New... Read more
Sale! B&H Photo drops prices on 13″ MacBo...
B&H Photo has new 13″ MacBook Pros on sale for up to $150 off MSRP, valid through January 16, 2019. Their prices are the lowest available for new 13″ MacBook Pros from any Apple reseller. These... Read more
Lowest prices ever on new 2018 11″ Apple iPad...
B&H Photo has new 2018 Apple 11″ iPad Pros in stock today and on sale for $50-$100 off MSRP. Shipping is free. These are the same iPad Pros offered by Apple in their retail and online stores. B... Read more
Sale! Get a new 27″ Apple iMac for $100-$200...
B&H Photo has new 27″ Apple iMacs on sale for up to $200 off MSRP. These are the same models offered by Apple in their retail and online stores. Shipping is free: – 27″ 3.8GHz 5K iMac (MNED2LL/A... Read more
Get a refurbished 2017 12.9″ iPad Pro for up...
Apple has Certified Refurbished 2017 12″ iPad Pros available for $120-$190 off original MSRP, depending on the model. An Apple one-year warranty is included with each model, outer shells are new, and... Read more

Jobs Board

Best Buy *Apple* Computing Master - Best Bu...
**667840BR** **Job Title:** Best Buy Apple Computing Master **Job Category:** Sales **Location Number:** 000456-Woodbridge-Store **Job Description:** **What does a Read more
*Apple* Enterprise Engineer - Evolver, Inc....
…contribute to the success of the company. Evolver is seeking an Apple Enterprise Engineer to work in Washington, DC Responsibilities: The successful candidate Read more
Geek Squad *Apple* Master Consultation Agen...
**667848BR** **Job Title:** Geek Squad Apple Master Consultation Agent **Job Category:** Services/Installation/Repair **Location Number:** 000531-Portland-Store Read more
Best Buy *Apple* Computing Master - Best Bu...
**665166BR** **Job Title:** Best Buy Apple Computing Master **Job Category:** Sales **Location Number:** 000868-West Lebanon-Store **Job Description:** **What does a Read more
Geek Squad *Apple* Master Consultation Agen...
**667309BR** **Job Title:** Geek Squad Apple Master Consultation Agent **Job Category:** Services/Installation/Repair **Location Number:** 001008-Vacaville-Store Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.