Add volume creation

This commit is contained in:
Terence Boldt 2021-06-09 08:23:18 -04:00
parent 0226b2dda2
commit 7e623c530f
8 changed files with 293 additions and 41 deletions

46
main.go
View File

@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/fs"
"os" "os"
"github.com/tjboldt/ProDOS-Utilities/prodos" "github.com/tjboldt/ProDOS-Utilities/prodos"
@ -14,36 +13,69 @@ func main() {
var pathName string var pathName string
var command string var command string
var outFileName string var outFileName string
var blockNumber int
var volumeSize int
var volumeName string
flag.StringVar(&fileName, "driveimage", "", "A ProDOS format drive image") flag.StringVar(&fileName, "driveimage", "", "A ProDOS format drive image")
flag.StringVar(&pathName, "path", "", "Path name in ProDOS drive image") flag.StringVar(&pathName, "path", "", "Path name in ProDOS drive image")
flag.StringVar(&command, "command", "ls", "Command to execute: ls, get, put, volumebitmap") flag.StringVar(&command, "command", "ls", "Command to execute: ls, get, put, volumebitmap, readblock, writeblock, create")
flag.StringVar(&outFileName, "outfile", "export.bin", "Name of file to write") flag.StringVar(&outFileName, "outfile", "export.bin", "Name of file to write")
flag.IntVar(&volumeSize, "volumesize", 65535, "Number of blocks to create the volume with")
flag.StringVar(&volumeName, "volumename", "NO.NAME", "Specifiy a name for the volume from 1 to 15 characters")
flag.IntVar(&blockNumber, "block", 0, "A block number to read/write from 0 to 65535")
flag.Parse() flag.Parse()
if len(fileName) == 0 { if len(fileName) == 0 {
fmt.Printf("Missing driveimage. Run with --help for more info.\n") fmt.Printf("Missing driveimage. Run with --help for more info.\n")
os.Exit(1) os.Exit(1)
} }
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
if err != nil {
os.Exit(1)
}
switch command { switch command {
case "ls": case "ls":
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
if err != nil {
os.Exit(1)
}
volumeHeader, fileEntries := prodos.ReadDirectory(file, pathName) volumeHeader, fileEntries := prodos.ReadDirectory(file, pathName)
prodos.DumpDirectory(volumeHeader, fileEntries) prodos.DumpDirectory(volumeHeader, fileEntries)
case "volumebitmap": case "volumebitmap":
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
if err != nil {
os.Exit(1)
}
volumeBitmap := prodos.ReadVolumeBitmap(file) volumeBitmap := prodos.ReadVolumeBitmap(file)
prodos.DumpBlock(volumeBitmap) prodos.DumpBlock(volumeBitmap)
case "get": case "get":
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
if err != nil {
os.Exit(1)
}
if len(pathName) == 0 { if len(pathName) == 0 {
fmt.Println("Missing pathname") fmt.Println("Missing pathname")
os.Exit(1) os.Exit(1)
} }
getFile := prodos.LoadFile(file, pathName) getFile := prodos.LoadFile(file, pathName)
os.WriteFile(outFileName, getFile, fs.FileMode(os.O_RDWR)) outFile, err := os.Create(outFileName)
if err != nil {
os.Exit(1)
}
outFile.Write(getFile)
case "readblock":
file, err := os.OpenFile(fileName, os.O_RDWR, 0755)
if err != nil {
os.Exit(1)
}
block := prodos.ReadBlock(file, blockNumber)
outFile, err := os.Create(outFileName)
if err != nil {
os.Exit(1)
}
outFile.Write(block)
case "create":
//fmt.Println("Create volume")
prodos.CreateVolume(fileName, volumeName, volumeSize)
default: default:
fmt.Printf("Command %s not handle\n", command)
os.Exit(1) os.Exit(1)
} }
} }

View File

@ -1,6 +1,8 @@
package prodos package prodos
import "os" import (
"os"
)
func ReadVolumeBitmap(file *os.File) []byte { func ReadVolumeBitmap(file *os.File) []byte {
headerBlock := ReadBlock(file, 2) headerBlock := ReadBlock(file, 2)
@ -27,11 +29,80 @@ func WriteVolumeBitmap(file *os.File, bitmap []byte) {
volumeHeader := parseVolumeHeader(headerBlock) volumeHeader := parseVolumeHeader(headerBlock)
for i := 0; i < len(bitmap)/512/8; i++ { for i := 0; i < len(bitmap)/512; i++ {
WriteBlock(file, volumeHeader.BitmapStartBlock+1, bitmap[i*512:i*512+513]) WriteBlock(file, volumeHeader.BitmapStartBlock+i, bitmap[i*512:i*512+512])
} }
} }
func CreateVolumeBitmap(numberOfBlocks int) []byte {
volumeBitmapBlocks := numberOfBlocks / 512 / 8
if volumeBitmapBlocks*8*512 < numberOfBlocks {
volumeBitmapBlocks++
}
// set all 1's to show blocks available...
volumeBitmap := make([]byte, volumeBitmapBlocks*512)
for i := 0; i < len(volumeBitmap); i++ {
volumeBitmap[i] = 0xFF
}
// boot blocks
MarkBlockInVolumeBitmap(volumeBitmap, 0)
MarkBlockInVolumeBitmap(volumeBitmap, 1)
// root directory
MarkBlockInVolumeBitmap(volumeBitmap, 2)
MarkBlockInVolumeBitmap(volumeBitmap, 3)
MarkBlockInVolumeBitmap(volumeBitmap, 4)
MarkBlockInVolumeBitmap(volumeBitmap, 5)
// volume bitmap blocks
for i := 0; i < volumeBitmapBlocks; i++ {
MarkBlockInVolumeBitmap(volumeBitmap, 6+i)
}
// blocks beyond the volume
totalBlocksInBitmap := volumeBitmapBlocks * 512 * 8
blocksBeyondEnd := totalBlocksInBitmap - numberOfBlocks
if blocksBeyondEnd > 0 {
for i := totalBlocksInBitmap - blocksBeyondEnd; i < totalBlocksInBitmap; i++ {
MarkBlockInVolumeBitmap(volumeBitmap, i)
}
}
//DumpBlock(volumeBitmap)
return volumeBitmap
}
func FindFreeBlocks(numberOfBlocks int) []int { func FindFreeBlocks(numberOfBlocks int) []int {
return nil return nil
} }
func MarkBlockInVolumeBitmap(volumeBitmap []byte, blockNumber int) {
bitToChange := blockNumber % 8
byteToChange := blockNumber / 8
byteToWrite := 0b11111111
switch bitToChange {
case 0:
byteToWrite = 0b01111111
case 1:
byteToWrite = 0b10111111
case 2:
byteToWrite = 0b11011111
case 3:
byteToWrite = 0b11101111
case 4:
byteToWrite = 0b11110111
case 5:
byteToWrite = 0b11111011
case 6:
byteToWrite = 0b11111101
case 7:
byteToWrite = 0b11111110
}
//fmt.Printf("blockNumber: $%04X byteToWrite: 0b%08b volumeBitmap: $%02X byteToChange: $%04X\n", blockNumber, byteToWrite, volumeBitmap[byteToChange], byteToChange)
volumeBitmap[byteToChange] &= byte(byteToWrite)
}

28
prodos/bitmap_test.go Normal file
View File

@ -0,0 +1,28 @@
package prodos
import (
"fmt"
"testing"
)
func TestCreateVolumeBitmap(t *testing.T) {
var tests = []struct {
blocks int
want int
}{
{65536, 8192},
{65533, 8192},
{140, 512},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d", tt.blocks)
t.Run(testname, func(t *testing.T) {
volumeBitMap := CreateVolumeBitmap(tt.blocks)
ans := len(volumeBitMap)
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}

22
prodos/block.go Normal file
View File

@ -0,0 +1,22 @@
package prodos
import (
"os"
)
func ReadBlock(file *os.File, block int) []byte {
buffer := make([]byte, 512)
file.ReadAt(buffer, int64(block)*512)
return buffer
}
func WriteBlock(file *os.File, block int, buffer []byte) {
WriteBlockNoSync(file, block, buffer)
file.Sync()
}
func WriteBlockNoSync(file *os.File, block int, buffer []byte) {
file.WriteAt(buffer, int64(block)*512)
}

130
prodos/format.go Normal file
View File

@ -0,0 +1,130 @@
package prodos
import (
"fmt"
"os"
"strings"
"time"
)
func CreateVolume(fileName string, volumeName string, numberOfBlocks int) {
if numberOfBlocks > 65535 || numberOfBlocks < 64 {
return
}
volumeName = strings.ToUpper(volumeName)
volumeNameLen := len(volumeName)
if volumeNameLen == 0 || volumeNameLen > 15 {
fmt.Println("bad name length")
return
}
file, err := os.Create(fileName)
if err != nil {
fmt.Printf("failed to create file: %s\n", err)
return
}
blankBlock := make([]byte, 512)
for i := 0; i < numberOfBlocks; i++ {
WriteBlockNoSync(file, i, blankBlock)
}
volumeHeader := [43]byte{}
// volume name
volumeHeader[0x04] = 0xF0 + byte(volumeNameLen)
for i := 0; i < volumeNameLen; i++ {
volumeHeader[i+0x05] = volumeName[i]
}
//creation date
creationDate := DateTimeToProDOS(time.Now())
for i := 0; i < len(creationDate); i++ {
volumeHeader[0x1C+i] = creationDate[i]
}
// access
volumeHeader[0x22] = 0xE3
// entry length
volumeHeader[0x23] = 0x27
// entries per block
volumeHeader[0x24] = 0x0D
// volume bitmap pointer
volumeHeader[0x27] = 0x06
// number of blocks
volumeHeader[0x29] = byte(numberOfBlocks & 0xFF)
volumeHeader[0x2A] = byte(numberOfBlocks >> 8)
file.WriteAt(volumeHeader[:], 1024)
file.Sync()
// boot block 0
WriteBlock(file, 0, GetBootBlock())
// pointers to volume directory blocks
for i := 2; i < 6; i++ {
pointers := make([]byte, 4)
if i == 2 {
pointers[0] = 0x00 // 0x00 indicates start block
} else {
pointers[0] = byte(i - 1)
}
pointers[1] = 0x00
if i == 5 {
pointers[2] = 0x00 // 0x00 indicated end block
} else {
pointers[2] = byte(i + 1)
}
pointers[3] = 0x00
file.WriteAt(pointers, int64(i*512))
}
// volume bit map starting at block 6
volumeBitmap := CreateVolumeBitmap(numberOfBlocks)
WriteVolumeBitmap(file, volumeBitmap)
file.Close()
}
func GetBootBlock() []byte {
bootBlock := []byte{
0x01, 0x38, 0xb0, 0x03, 0x4c, 0x1c, 0x09, 0x78, 0x86, 0x43, 0xc9, 0x03, 0x08, 0x8a, 0x29, 0x70,
0x4a, 0x4a, 0x4a, 0x4a, 0x09, 0xc0, 0x85, 0x49, 0xa0, 0xff, 0x84, 0x48, 0x28, 0xc8, 0xb1, 0x48,
0xd0, 0x3a, 0xb0, 0x0e, 0xa9, 0x03, 0x8d, 0x00, 0x08, 0xe6, 0x3d, 0xa5, 0x49, 0x48, 0xa9, 0x5b,
0x48, 0x60, 0x85, 0x40, 0x85, 0x48, 0xa0, 0x5e, 0xb1, 0x48, 0x99, 0x94, 0x09, 0xc8, 0xc0, 0xeb,
0xd0, 0xf6, 0xa2, 0x06, 0xbc, 0x32, 0x09, 0xbd, 0x39, 0x09, 0x99, 0xf2, 0x09, 0xbd, 0x40, 0x09,
0x9d, 0x7f, 0x0a, 0xca, 0x10, 0xee, 0xa9, 0x09, 0x85, 0x49, 0xa9, 0x86, 0xa0, 0x00, 0xc9, 0xf9,
0xb0, 0x2f, 0x85, 0x48, 0x84, 0x60, 0x84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0xa5, 0x50, 0x18, 0x20, 0x6f, 0x09,
0xd0, 0xe3, 0xa0, 0x7f, 0x84, 0x52, 0x08, 0x28, 0x38, 0xc6, 0x52, 0xf0, 0xce, 0x18, 0x08, 0x88,
0xf0, 0xf5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
return bootBlock
}

View File

@ -1,13 +0,0 @@
package prodos
import (
"os"
)
func ReadBlock(file *os.File, block int) []byte {
buffer := make([]byte, 512)
file.ReadAt(buffer, int64(block)*512)
return buffer
}

View File

@ -1,7 +1,6 @@
package prodos package prodos
import ( import (
"fmt"
"time" "time"
) )
@ -21,8 +20,6 @@ TIME: | hour | | minute |
*/ */
func DateTimeToProDOS(dateTime time.Time) []byte { func DateTimeToProDOS(dateTime time.Time) []byte {
fmt.Printf("Sending date/time...\n")
year := dateTime.Year() % 100 year := dateTime.Year() % 100
month := dateTime.Month() month := dateTime.Month()
day := dateTime.Day() day := dateTime.Day()

View File

@ -1,15 +0,0 @@
package prodos
import (
"fmt"
"os"
)
func WriteBlock(file *os.File, block int, buffer []byte) {
fmt.Printf("Write block %d\n", block)
file.WriteAt(buffer, int64(block)*512)
file.Sync()
fmt.Printf("Write block completed\n")
}