Hibernate between OS X and Bootcamp Win 7

22

9

Is it possible to use the true hibernate system functionalities of Win 7 and OS X to create a kind of instant switching between the two? Imagine this when reading/writing the sleep images to a quick SATA-3 SSD.

Hibernate-corruption-fix: If this is made possible, make sure that your different partitions are not write-enabled from eachother. (e.g. make it so that Boot Camp Windows 7 cannot write to OS X partition and vice versa). This safety measure should make it impossible for your different systems to corrupt eachother when one is suspended into hibernation.

Edit: I realized that I'm not sure that this is how partitions work on a disk. The hibernated OS X kernel could have references to blocks on the disk which it will continue to write to when it is woken up again. Are disk blocks like these contained within a partition or are they assigned to a partition after they have been written on? Is it different on HDD and SSD? Does this affect the above hibernate-corruption-fix?

Ok let's move on:

Windows 7 already has an option "Hibernate" which allows you to boot back to your OS X partition, but OS X does not exactly offer the same.

With OS X it is possible to hibernate by changing the hibernatemode system variable.

Pre OS X 10.7

sudo pmset -a hibernatemode 2

OS X 10.7

sudo pmset -a hibernatemode 25

This makes your Mac go into hibernate when you put it to sleep. You can put it to sleep with:

sudo shutdown -s now

Ok, so far so good. So we can put both Windows 7 and OS X Lion into hibernation at will. But wait! There's more problems up ahead.

When you put OS X into hibernate it will automatically boot back into OS X when you power your Mac on again. This makes it impossible for us to use it for "hotswitching" between partitions.

Possible fixes:
If you install rEFIt without rEFItBlesser you will get the rEFIt boot menu after OS X hibernation. On OS X 10.7 Lion however, this does not fully work, instead you get a black screen and have to hard shutdown your Mac. But hey! At least it interrupts the automatic OS X boot, right? Maybe it can be tweaked further from there.

As mentioned on earlier OS X:s the rEFIt boot menu actually does come up after hibernation shutdown + power on. This allows you to e.g. boot your Boot Camp Windows 7 partition instead. There are problems here as well though. Apparently the OS X partition loses its hibernation flag if you follow this method, which means that the next time you boot into OS X, it will not return from its hibernation state. Instead it will do a clean startup. Why is this?
Does OS X look for the existence of a sleep image to determine wether to wake up from hibernation or to do a clean start or does it have some kind of system variable/flag set?

Summary

When Win 7 goes into hibernate it shuts down completely and you can then boot into OS X on startup. On OS X however, hibernate forces you to wake up into OS X. Can you hack this so that you're allowed to select boot partition after OS X hibernates and still be able to return later to OS X and wake it up from its hibernate?

Let's see what we collectively can come up with!

Willem

Posted 2012-06-05T10:34:09.980

Reputation: 544

@zneak For the Linux system that I currently have, if I only hibernate from Windows instead of shutting it down, before switching to Linux, I can't write to the Windows partition. Only reading is possible. I think this partly solves the issue. – xji – 2019-03-11T10:57:12.580

see http://superuser.com/questions/59188/does-mac-os-x-support-hibernation for hibernating in OSX ( = your 2) - note the default is as per your 3 as you have seen

– user151019 – 2012-06-05T10:59:45.037

I tried those solutions before but they do not change anything for me. My Mac still goes into Safe Sleep regardless of what setting I use or if i use the Deep Sleep Widget mentioned. It wakes up instantly without showing the loading bar at first which I assume means that is was never in real hibernation mode? – Willem – 2012-06-05T11:09:35.117

1After I asked my question on Ask Different, I was told that the reason the feature was dropped from Mac OS is because it's actually dangerous. When your computer wakes up, it expects the environment to be identical to when it went to sleep. Suppose you have a file open on your Windows partition, sleep Mac OS, and delete that file from Windows. You might be in for some trouble. Since the ability to run Windows on a Mac is a big feature of Intel Macs, Apple probably chose to make Windows-like hibernation difficult to protect novice users. – zneak – 2012-06-06T14:04:45.057

I actually proposed a solution to that problem in this text. Look near the beginning. – Willem – 2012-06-06T22:15:27.960

What's your question again? It looks like a feature request to Apple (?) interspersed with questions that are unrelated to each other. – Daniel Beck – 2012-06-07T09:08:21.533

The full questions is summarized at the bottom under "Summary".
I've heard that Apple announced a feature like this for one of the Leopards but took it away before launch saying that they will not do it because of data security problems. The questions within the question are very much related to each other and the main question itself. Edit: I replaced the somewhat fuzzy question at the beginning of the text with the more concrete one from the summary.
– Willem – 2012-06-07T09:13:36.917

If I'm reading pmset(1) correctly, sudo pmset -a hibernatemode 2 does essentially nothing: 2 (bit 1) only works with 1 (bit 0), since all bit 1 means is "wait until running out of batteries before powering off"....

– SamB – 2012-06-20T19:32:38.160

@Willem How do you hibernate in windows and start in OSX? – shoosh – 2013-07-25T06:19:22.233

Answers

18

It is not possible to do this the way you are thinking. The problem lies in the tight integration of Apple's firmware and OS X. OS X and the firmware work with each other to determine the sleep state of the computer.

When Windows hibernates, it dumps the contents of RAM to C:\Hiberfil.sys and sets a flag in the registry that the machine is hibernated. When you first boot a Windows machine, the boot sector code loads the BCD file, which loads that portion of the registry very early in the boot process and sees that the system is hibernated. After performing a basic sanity check, it loads hiberfil.sys back into memory. The important thing here is that all all of this is contained within the filesystem. This is why you can freely boot to OS X, then boot again back to Windows and it will continue to resume from the hibernation file.

The same is not true of OS X. When OS X hibernates, it dumps the contents of RAM to /var/vm/sleepimage the same way Windows does. But it saves the hibernation flag in the PRAM, not the filesystem (the setting is called IORegistryCurrentSleepMode if you're interested). When you turn a Mac back on, the values in PRAM are read before an attempt is even made to boot to the OS. If the flag indicates that the system is hibernated, the first thing it does is flip it back to a normal status. The firmware then immediately boots the system and ignores the Startup Disk preference and any attempts to Option+boot. You don't even get a startup chime. On a Mac, the firmware contains all the logic needed to inspect the filesystem and boot the OS. It does not need boot code the way Windows does.

When you throw rEFIt into the mix, it inserts itself into the process. It replaces the normal /System/Library/CoreServices/boot.efi (which is OS X's boot loader), with its own boot loader file. This is where things get fuzzy for me because this is all Apple proprietary stuff, but the bottom line is that when the firmware is booting OS X, it passes whatever arguments needed to load /var/vm/sleepimage instead of the normal Darwin kernel. rEFIt does not do this properly with Lion and later. But regardless of whether it's an older version of OS X or a newer one, the firmware has already flipped the hibernation bit before rEFIt even loads. This is why resuming OS X from hibernate is no longer possible after the first power-on.

Wes Sayeed

Posted 2012-06-05T10:34:09.980

Reputation: 12 024

Wow. Finally an answer and a good one at that! (Even though the news is kind of sad...)

Would it be possible to solve this by having Windows set the PRAM flag as it goes into hibernation? – Willem – 2014-09-25T21:21:30.100

If you could find a windows-based program for modifying PRAM values (similar to OS X's nvram tool) I think it'd be possible. You could set it as a scheduled task to trigger when the workstation sleeps. But I don't know if such a program exists. If you run across one I'd love to hear about it. – Wes Sayeed – 2014-09-25T21:57:22.013

Maybe someone here on Stackoverflow is interested in writing one if we highlight it. I believe the possibility of switching "instantly" between OS X and Windows is sought after by many. – Willem – 2014-09-25T23:13:36.600

1

I posted the question here because I'm interested too. It might not be possible though. The firmware provides a Compatibility Support Module (CSM) which emulates a PC BIOS so Windows can boot. Windows may not be able to see through that CSM. Your screen brightness, keyboard backlight brightness, and audio volume saves to the PRAM as well. But I notice when I change those things from Windows they do not get retained on the next reboot the way they do when I change them from OS X.

– Wes Sayeed – 2014-09-25T23:35:00.660

Alright nice, let's hope it's possible. I'm accepting your answer in the meantime. Let's comment here if we find anything. – Willem – 2014-09-26T07:10:24.230

I wonder if rEFInd (successor project of rEFIt) could be made to reconginze the parameters passed by firmware to resume from OS X hibernation and give the user the option to resume from hibernation or boot annother OS. If the user chooses annother OS rEFInd should reset the PRAM flags needed to resume from hibernation later before booting the selected OS. I have not found any mention of hibernation on the rEFInd website. http://www.rodsbooks.com/refind/index.html

– robartsd – 2015-09-11T18:20:35.720