Mac OS 9 Lives

Classic Mac OS Software => Hacking the System, Mac OS 9.3, and Beyond ! => Topic started by: Greystash on March 12, 2019, 01:40:50 AM

Title: Setting PowerBook G4 fan speed in open firmware
Post by: Greystash on March 12, 2019, 01:40:50 AM
Is there anyone out there with some knowledge on Open Firmware?

I've been playing around with OF recently to try turning the fans on my PowerBook G4 while running OS9. I've hit a roadblock trying to modify values within the 'fan' device, each time I try to modify a property OF returns "no current instance"

I can change values for other devices, the example below turns all LEDs on:

Code: [Select]
dev via-pmu
ledson true

Modifying fan properties example:
Code: [Select]
dev fan
fans-on true

RETURNS
> no current instance

I found this article (https://lists.apple.com/archives/darwin-dev/2008/Oct/msg00017.html) where somebody enabled the fans on their Pismo, but I haven't been able to get close to this.

Below is a list of the 'words' relevant to the fan device.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on March 12, 2019, 04:12:47 AM
Most OF devices insist that you "open" them and create and instance.

Code: [Select]
" fan" open-dev constant fan-ih
" fans-on" fan-ih $call-method

Once you are done with an opened device, you can close it. It doesn't really matter if you do.
Code: [Select]
fan-ih close-dev
I don't know how to turn on the fan, but the "see" word lets you look inside the OF routines.
Code: [Select]
dev fan
see fan-on
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: darthnVader on March 12, 2019, 11:47:16 AM
I don't want to hijack the thread, but I figure I'll side track it a little.

Something me and Elliot have been working on, iBooks and Powerbooks with the 7447a boot in dfs-low mode, and because OS 9 has no power management software for the 7447a the CPU runs at 50% under OS 9.

We've been working on fixing this through the Mac OS ROM, but this thread makes me think maybe we could do it with an nvram script, and I've found some things that make me think there is a way to do what we want...

Code: [Select]
Trying 10.1.2.3...
Connected to 10.1.2.3.
Escape character is '^]'.
 ok
0 > dev /cpus/@0  ok
0 > words

set-dfs-high    translate       modify          unmap           map
release         claim           set-dfs-low     set-dfs-high    spd
close           open  ok
0 > spd 140  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173712  ok
0 > set-dfs-high  ok
1 > spd 140  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173712  ok
1 > modify
STACK UNDERFLOW!
 ok
0 > claim
CLAIM failed
 ok
0 >

set-dfs-high is what we are looking for, but I'm unsure how to use the word?

Seems it would also use the "modify"  or maybe the "claim" word, or "translate"?

Code: [Select]
0 > " /cpus/@0" open-dev constant set-dfs-high  ok
0 > " translate" set-dfs-high $call-method  ok
0 > set-dfs-high close-dev  ok
0 > spd 140  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173712  ok
0 >

That didn't work, as we can see from "spd" HID1 bit 9 is still set to true, but I'm really close, and just need a little help getting the correct syntax, Daniel?
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: darthnVader on March 12, 2019, 12:12:26 PM
Never mind, it couldn't be simpler.

Code: [Select]
Trying 10.1.2.3...
Connected to 10.1.2.3.
Escape character is '^]'.
 ok
0 > dev /cpus/@0  ok
0 > spd 139  pll-ratio*2 = 10  HID1 = 80416c80  DelayAACK = 0  GPIO1 = 4 BusClk = 133173483  ok
0 > set-dfs-high  ok
0 > spd 70  pll-ratio*2 = 20  HID1 = 80015c80  DelayAACK = 0  GPIO1 = 7 BusClk = 133173483  ok
0 >

Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Greystash on March 13, 2019, 12:57:13 AM
Most OF devices insist that you "open" them and create and instance.

Code: [Select]
" fan" open-dev constant fan-ih
" fans-on" fan-ih $call-method

Once you are done with an opened device, you can close it. It doesn't really matter if you do.
Code: [Select]
fan-ih close-dev
I don't know how to turn on the fan, but the "see" word lets you look inside the OF routines.
Code: [Select]
dev fan
see fan-on


Thanks Daniel! Your code turns the fans on at full speed which is a fantastic start!

However, I can't figure out how to work with any of the routines. There's a lot of options that look like they can be modified but it's a matter of figuring out the correct syntax to overwrite the default values. For example 'set-speed' and '.set-speed' give very different output when using the 'see' method and don't give much indication on what can be edited:

Code: [Select]
> see .speed
1 read-speed cr " TACH1 = " type .d 2 read-speed cr " TACH2 = " type .d 3 read-speed cr " TACH3 = " type .d 4 read-speed cr " TACH4 = type .d ; ok

Code: [Select]
> see set-speed
>r 40 dup read-reg 8 andc write-reg case
    1 of
      5c dup read-reg e0 or write-reg 30 r> write-reg
      endof
    2 of
      5d dup read-reg e0 or write-reg 31 r> write-reg
      endof
    3 of
      5e dup read-reg e0 or write-reg 32 r> write-reg
      endof
    dup of
      5e dup read-reg e0 or write-reg 32 r> write-reg
      endof
  endcase
; ok

Code: [Select]
> see set-speeds
1 over set-speed 2 over set-speed 3 swap set-speed

I'll keep mucking around with it but if anyone can offer any hints it would be greatly appreciated!
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on March 13, 2019, 03:35:27 PM
Looks like 'set-speed' takes an index of what speed to set and the number to set the speed to.
Code: [Select]
1 2a " set-speed" fan-ih $call-method
That would set speed variable 1 to the value 0x2a (42 in decimal). There seem to be 3 speed variables. Who knows what they do?

I would suggest turning the fan on, then using '.speed' to see what the different speed values are. Then try using set-speed to change one of the values with one that is close to it. If the fan noise changes, you have found out something.

That stuff with 'case' and 'of' and 'endof' and 'endcase' is a switch statement.
Code: [Select]
: test-switch case
0 of ." 0 selected" cr endof
1 of ." 1 selected" cr endof
dup of ." something else selected" cr endof
endcase
;
0 test-switch
1 test-switch
51F244 test-switch
'dup of' is a tricky way to match any possible value in the switch statement.

'>r' puts a value onto the return stack and 'r>' moves it back to the data stack. It is a simple way to leapfrog data over a bunch of code. It can mess up loops and function returns if used incorrectly, so beware.

I think that should be enough for you to figure out what is happening. If all else fails, make small test functions that contain just the things you want to figure out. Mess with them and see what happens.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Greystash on March 13, 2019, 06:07:31 PM
Looks like 'set-speed' takes an index of what speed to set and the number to set the speed to.
Code: [Select]
1 2a " set-speed" fan-ih $call-method
That would set speed variable 1 to the value 0x2a (42 in decimal). There seem to be 3 speed variables. Who knows what they do?

I would suggest turning the fan on, then using '.speed' to see what the different speed values are. Then try using set-speed to change one of the values with one that is close to it. If the fan noise changes, you have found out something.

That stuff with 'case' and 'of' and 'endof' and 'endcase' is a switch statement.
Code: [Select]
: test-switch case
0 of ." 0 selected" cr endof
1 of ." 1 selected" cr endof
dup of ." something else selected" cr endof
endcase
;
0 test-switch
1 test-switch
51F244 test-switch
'dup of' is a tricky way to match any possible value in the switch statement.

'>r' puts a value onto the return stack and 'r>' moves it back to the data stack. It is a simple way to leapfrog data over a bunch of code. It can mess up loops and function returns if used incorrectly, so beware.

I think that should be enough for you to figure out what is happening. If all else fails, make small test functions that contain just the things you want to figure out. Mess with them and see what happens.


Thanks so much Daniel! I can change the fan speed by changing the hex value which is exactly what I want. I've tried adding the configuration I want to an NVRAM script with nvedit and use-nvramrc, and it sort of works when the PB is turned on but the fans stop as soon as it starts looking for the system. I'm guessing this is because the instance is no longer open/running when the PB has finished with Open Firmware?

Is there any way to get these settings to persist through boot?

This is my script:

Code: [Select]
nvedit

dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end

dev fan
" fan" open-dev constant fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
device-end

Exit with ctrl+c

Code: [Select]
nvstore
setenv use-nvramrc? true
reset-all

I've also tried specifying each 'fan' alias as it's full path but that doesn't work.

If I run the commands outside of the nvram editor and run `mac-boot` the fans continue to run at the speed I've specified
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Greystash on March 13, 2019, 09:59:37 PM
Since I haven't been able to get the fan speeds to work within the nvram properly I've just written a script with everything I need, and this is easier than typing the fan config each boot.

Code: [Select]
\ comment
cr
dev fan
" fan" open-dev constant fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
cr
dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end
mac-boot

Each time I want the fans to run when I'm using the PB I need to boot into OF, then execute the script ('fans' which was written in SimpleText):
Code: [Select]
boot hd:\fans
Is there a way to either force Open Firmware to load each boot, or to get this code working within the nvram?
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on March 14, 2019, 12:51:37 PM
Since I haven't been able to get the fan speeds to work within the nvram properly I've just written a script with everything I need, and this is easier than typing the fan config each boot.

Code: [Select]
\ comment
cr
dev fan
" fan" open-dev constant fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
cr
dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end
mac-boot

Each time I want the fans to run when I'm using the PB I need to boot into OF, then execute the script ('fans' which was written in SimpleText):
Code: [Select]
boot hd:\fans
Is there a way to either force Open Firmware to load each boot, or to get this code working within the nvram?

The boot-device nvram variable can be used to boot from a file.
Code: [Select]
setenv boot-device hd:\fansIt is recommended that you make the boot script boot to the previous boot device (likely "hd:,\\:tbxi"). Just putting 'mac-boot' at the end would make it boot itself again and again and again if it was set as the boot-device.

As for getting it to work in nvram, I suspect that something resets the fans once the boot starts. Maybe make a function in nvram that starts the fans, and then call that function in the boot command?
Code: [Select]
nvedit

dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end

0 value fan-ih
: activate-fans
" fan" open-dev to fan-ih
1 384 " set-speed" fan-ih $call-method
2 384 " set-speed" fan-ih $call-method
;
Then change the boot-command nvram variable so activate-fans is called.
Code: [Select]
setenv boot-command activate-fans mac-boot
There are other ways of doing it, like building ELN's NewWorld-rom repo and adding your code to the boot script.

If you really want to mess with stuff, try putting your code into a 'Apple_Driver_OpenFirmware' partition. If a filesystem is accessed from OF, the '/packages/mac-parts' package will look for a Apple Partition Map partition of type 'Apple_Driver_OpenFirmware' on the device. If it finds one, it will treat the contents of the partition as either raw forth or fcode and try to run it. It expects the code to properly create a device package which can be passed the device accesses, but it can be used to just run arbitrary code when the disk is first used. Try sticking fan code there  ;D
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Greystash on March 15, 2019, 02:01:33 AM
Thanks so much Daniel!! For now I went with the boot from file method and that's working perfectly :D
I'll take a look at adding this to a ROM file and experiment with the 'Apple_Driver_OpenFirmware' partition method. I'll post an update once I've sorted experimented with all of the options.

Once again thank you so much for the help it's much appreciated! :)
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on March 15, 2019, 01:48:21 PM
No problem. It's very fun :)

Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on June 16, 2020, 01:32:22 PM
Hello,
Any updates? I have recently picked up an iBook G4 1.2GHz and well, it runs hot. Like really really hot...
Have you found a permanent solution to triggering fans that could be added as an nvram parameter to work after every reboot?

Thanks in advance.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Greystash on June 16, 2020, 03:25:03 PM
I haven't had a chance to dig into editing the Mac OS ROM, but here's the script I run from Open Firmware. This has been a permanent fix for me since I have working batteries and the unit never loses power. I don't have my machine on hand at the moment to double check but this should be correct.

bootscript (SimpleText file which is located in the HDD root directory)
Code: [Select]
\ comment
cr
dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end
" fan" open-dev constant fan-ih
1 340 " set-speeds" fan-ih $call-method
boot hd:,\\:tbxi

Description:
Lines 1 - 2: Scripts must begin with a 'comment' and a carriage return (cr)
Lines 3 - 5: Changes the CPU version to enable OS9 booting on unsupported machines
Line 6: Opens/creates an instance of the 'fan' device
Line 7: Sets the speed for both fans. '340' is the speed, which may need to be altered to best suit your machine/preference.
Line 8: Continue booting the system

To run this script on every boot you need to boot into Open Firmware and do this once:
Open the NVRAMRC editor
Code: [Select]
nvedit
Set the boot device to your bootscript
Code: [Select]
setenv boot-device hd:\bootscriptClose the NVRAMRC editor by pressing CTRL + C
Run the following commands
Code: [Select]
nvstore
setenv use-nvramrc? true
reset all

If your machine looses power you will need to run these commands again
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on June 16, 2020, 04:37:08 PM
Hi, thanks for your answer!
I have actually found the same solution online, but I thought there's been a better way discovered. The script method is good enough for now, though - still much better than nothing.

Btw, I have tried using the same method on my 12" 867MHz Powerbook, but well, it looks like it requires a different solution - do you know how to perform the same trick on a 7455 cpu?
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on June 17, 2020, 03:28:25 PM

Code: [Select]
\ comment
cr
dev /cpus/PowerPC,G4@0
80010201 encode-int " cpu-version" property
device-end
" fan" open-dev constant fan-ih
1 340 " set-speeds" fan-ih $call-method
boot hd:,\\:tbxi

Btw, since the last line forces Mac to boot from the first available boot partition (rendering Startup Disk control panel non-working) I wonder if there is a command to finish this script with that wouldn't direct to a one, fixed boot partition?

I switch between OS9 and OS X quite often, many times simply staying on OS X for several days straight, thus I heavily rely on Startup Disk control panel (OS9) and Startup Disk setting (OS X), that's why I ask.

I realize you can press Option button on power up to switch between boot partitions, but it's not that convenient and I'd have to do that every single time I'd like to use OS X. Small inconvienience, but still - do you know if there is a way to fix it?

Thanks in advance!
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: macarone on June 18, 2020, 05:59:00 AM
I realize you can press Option button on power up to switch between boot partitions, but it's not that convenient and I'd have to do that every single time I'd like to use OS X. Small inconvienience, but still - do you know if there is a way to fix it?

I have the same question.

In different words how can one get "Boot Manager: (Option key) and  "Startup disk" (Prefernce Pane/Control  Panel) in synch.

The contribution  I can make is that when  using "Boot Manager",  you can save some waiting time once the partition you want appears, by clicking the Mouse once and then Command-Period.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: IIO on June 18, 2020, 07:05:00 AM
there are many ways how to boot into a particular system but none of them is as easy as holding a key after start.

like macarone says you can pause the search on boot manager (unlinke in the controlpanel before rebooting) but it still includes waiting time, several clicks, and the risk that you sometimes choose the wrong OS by accident.

a reboot command, no matter how it is triggered (keyboard, finder menu, apple script, console...), will always boot into the currently chosen startup folder as set per PRAM.

for most people rebooting from X to X is the lesser case compared to rebooting from 9 to 9, so your best bet is to always choose your (main, if you have many) 9 folder as startup folder and then remember, that if you want to boot (or reboot) into OSX or linux, you have to go via the boot manager.

OS9 = default case, OSX = hold alt. easy to remember.

now what you could do when your machines doesnt support the boot manager or when you want to control a reboot by remote, no idea. :)

(some machines will also support holding D to boot from the very first partition. or to hold X to boot from the first partition with OSX. but which one is that? ... and it can easily stop working when your disks and volumes setup changes.)

what would be a neat solution to easily choose 1 out of 2 systems would be that you

1. always had the list of currently available boot folders ready somewhere

2. then it should show up somewhere in the finder menu
extras
/restart from disk 1 folder 1
/restart from disk 1 folder 2

but what would you save? now the search for current bootable systems happens everytime when the finder is loaded.

Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on June 18, 2020, 09:25:16 AM

a reboot command, no matter how it is triggered (keyboard, finder menu, apple script, console...), will always boot into the currently chosen startup folder as set per PRAM.

Exactly, that's the issue: how to make it boot into partition chosen by Startup Disk (regardless if under OS 9 or X) and not the very first bootable one, as forced by boot tbxi command? Is there an OF command that makes mac boot without specifying a fixed boot partition?
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on July 19, 2020, 07:49:26 AM
I wrote a little NVRAM script for you. It will make sure that the fan script is always booted first, regardless of what the boot device actually is.

Code: [Select]
' mac-boot @ constant saved-word

: fake-mac-boot
  saved-word ' mac-boot code!
  boot hd:42,\path\to\fan\script.of
;

' mac-boot ' fake-mac-boot brpatch

Stick this in NVRAM. The first call to mac-boot (done by the system) will boot the fan script. The second call to mac-boot (which you should put at the end of the fan script) will go and boot whatever mac-boot would normally boot.w

As long as nvram isn't disturbed, the fan script will always be run first.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on July 23, 2020, 02:04:38 PM
Code: [Select]
' mac-boot ' fake-mac-boot brpatch

The second call to mac-boot (which you should put at the end of the fan script) will go and boot whatever mac-boot would normally boot.

So this will basically re-enable the Startup Disk utility and let me boot to whichever partition I chose in Startup Disk (regardless if on os9 or x)?
Thanks, I'll try this as soon as possible :)
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on July 23, 2020, 04:32:02 PM
I messed up the script a bit. Here is the modified version.
Code: [Select]
' mac-boot @ constant saved-word

: fake-mac-boot
  saved-word ' mac-boot code!
  " boot hd:42,\path\to\fan\script.of" evaluate
;

' mac-boot ' fake-mac-boot brpatch

The word boot always grabs the boot path from the text in front of it at runtime, so directly compiling it in a function is a mistake. The Forth interpreter would have treated the path as forth code, and boot would have tried to grab text from who knows where.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on July 26, 2020, 05:55:06 PM
Looks like my English isn't enough to get an answer from you said, so I would much appreciate of you could tell me (yes or no) whether this piece of code would reenable the Startup Disk control panel or, again, it forces a Mac to boot from a specific, fixed partition every single time?
Thanks!
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on July 26, 2020, 06:06:09 PM
Yes, the code will reenable Startup Disk.

Stick the piece of code in NVRAM. Then modify your fan script so it has mac-boot at the end instead of boot.

You will have to make sure the boot-device NVRAM property doesn't point to your fan script. Just point it to the normal hd:,\\tbxi and use Startup Disk normally.

You don't need to worry about not understanding what I said in the last message. It was extreme tech-speak.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on November 21, 2020, 08:31:38 PM
Yeah, not only it didn't work for me, but also bricked my iBook.
Thanks nonetheless.

I'll be back to my previous method granted I somehow unbrick my iBook...
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Daniel on November 21, 2020, 08:52:39 PM
Yeah, not only it didn't work for me, but also bricked my iBook.
Thanks nonetheless.

I'll be back to my previous method granted I somehow unbrick my iBook...

Try holding down the power button for a very long time. On some devices OF will ignore the NVRAM and boot to a "recovery mode". Then you can clear the changes with reset-nvram.

iMic found out about it here. http://macos9lives.com/smforum/index.php?topic=2138.0 (http://macos9lives.com/smforum/index.php?topic=2138.0)

I think there's some article on it somewhere, but I can't find it.
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: LarsG5 on November 21, 2020, 09:03:12 PM
I'll give it a try, thanks!
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: ahihi on December 20, 2023, 12:54:47 PM
many thanks to everyone for all the good info in this thread!

i wanted a quick way to boot my OS9 partition with a specified fan speed, so i spent some quality time with OF and came up with the following...

firstly, i found out that the range of speeds accepted by the set-speeds method seems to be 0 to ff (OF defaults to hexadecimal, so that would be 0 to 255 in decimal). this is the case on my PowerBook6,7 (iBook G4 2005), dont know about others. larger numbers just wrap around to this range. it is worth noting that this is different from the numbers reported by .speed, which look like they might be RPMs.

with that in mind, i wrote this routine and added it to my nvramrc:

Code: [Select]
: fans9
dup 0 ff between if
    " set-speeds" " fan" open-dev dup >r $call-method r> close-dev
    " hd:\\,tbxi" boot
  else
    ." speed must be between 0 and ff"
    drop
  then
  ;

now, i can boot into OF and simply type

Code: [Select]
ff fans9

to boot from my OS9 partition with fans at full speed! or perhaps 80 fans9, for 50%-ish speed, etc.

note that this is hardcoded to boot hd:\\,tbxi, you may have to change that for your system. and as always, run at your own risk :)
Title: Re: Setting PowerBook G4 fan speed in open firmware
Post by: Greystash on December 28, 2023, 01:05:51 AM
Thanks ahihi that's fantastic! Looking forward to trying this out  8)