Pages

6/19/2012

Ruby Script For Downloading OS X's Boot Camp

My situation was to installing a Windows7 on to Mac Book Pro 13".
It should be doing fine because of the Boot Camp application that pre-installed in every OS X (in /Applications/Utilities) and it will need just few clicks to be done. Unfortunately, during the process, the BootCamp application needs to download Boot Camp drivers (they are Mac devices' driver for Windows) directly from Apple website and I can't download it because of bad internet access. Another way around is to skip the download, and use OS X installation DVD to install the drivers into Windows when it finishes installation. Somehow the MacBook I got doesn't come with any OS X Lion DVD. I can't do it this way.


Then I searched on the Internet for a way to download the drivers directly (trying to by-pass the application). I found nothing useful on Apple website. Apple's download page only has Boot Camp update files but not the actual installation file for the first-time users. No hope in doing it for me.


Further I searched, I found this blog. It has a pretty detail information on how to directly download the Boot Camp drivers. However, there is a step that would takes me a lot of time to do it if I have to do it manually. Therefore, I wrote this script for doing the heavy task for me.


Let me describe the basic process first:

  1. First, you need to see the model of your Mac, go to "Apple menu" -> "About this Mac" -> "More Info…" -> "Hardware". Remember the model and number (e.g. "MacBookAir3,1"), it will be use later. 
  2. you need to download this file http://swscan.apple.com/content/catalogs/others/index-lion-snowleopard-leopard.merged-1.sucatalog . It's a Mac update file list.
  3. Open the file in a good memory-management text-editor application, since the file is quite large for Notepad. (the file's size is 2.1 MB, and it could crash Notepad)
  4. search for a URL in the file that has word BootCampESD.pkg in it. And keep all matched lines.
  5. For each location of the matched line, in few lines below it, there is another URL that has the word "English.dist" (make sure the version (041-XXXX) from both URL are matched)
  6. Use a browser to go to .dist URL, it will contains javascript at the top of the HTML. On the top of the HTML page, there is a line contains information of the model that needs this download, such as:  var models = ['MacBookAir3,1','MacBookAir3,2',];
  7. Check to see if your model:
    1. Matches: download the .pkg file( it would be around ~600 MB), then proceed to next step (8)
    2. doesn't match what's in the Javascript, both these .dist and .pkg can be ignore. And repeat from step 4. again until there is a match
  8. From the file "BootCampESD.pkg", use 7zip(http://www.7-zip.org/) to browse into the file under Library/Application Support/BootCamp/ , then extract a file called WindowsSupport.dmg out
  9. Further use 7zip to browse inside "WindowsSupport.dmg" and there is another file 0.Apple_ISO and this is the file we are looking for. Extract it out and rename it to have suffix as ".iso" ; e.g. BootCamp.iso
  10. Mount BootCamp.iso on Windows or write it to a  or USB
  11. and now you can install drivers from the drivers resided in the ISO file.

As you can see, there are a hard-working process from steps 4 to 7, since I found that it has about 6 iterations I have to do it. So I thought I would write a ruby script for processing it for me, it would be pretty convenient. I was planing on writing every steps into my script at first, but I can't find a way to extract the "BootCampESD.pkg" yet. So, my script only does step 2-7 for now.

Here's the code
#!/usr/bin/env ruby

# Author: Anidear (anidear1 {at} gmail {dot} com)
# My Blog: http://blog.anidear.com
# Credit: http://blog.timvalenta.com/2011/11/19/boot-camp-driver-downloads/

require 'net/http'
require 'uri'

MAC_UPDATE_URL = 'http://swscan.apple.com/content/catalogs/others/index-lion-snowleopard-leopard.merged-1.sucatalog'

mac_update_uri = URI(MAC_UPDATE_URL)
mac_update_xml = Net::HTTP.get(mac_update_uri)
dist_links = Hash.new
pkg_links = Hash.new
mac_update_xml.split("\n").each{|line|
    #search for 041-XXXX.English.dist or BootCampESD.pkg
    line.chomp.scan(%r{(http://[^<]*041-[^<]*\English.dist)|(http://[^<]*BootCampESD\.pkg)})
        .each{|link|
            link_url = (link[0].nil?)? link[1] : link[0] ;
            version = link_url.match(/041-[0-9]+/)[0]
            if (link[0].nil?)
                pkg_links[version] = link_url
            else
                dist_links[version] = link_url
            end
        }
}

#filter only .pkg presents
dist_links.select!{|key,val|
    pkg_links.key?(key) #keep dist_link if pkg_link presents
}

#for each of dist_links
#find something like 
# "var models = ['MacBookAir3,1','MacBookAir3,2',];"
list = dist_links.keys.sort
list.each{|key|
    val = dist_links[key]
    uri = URI(val)
    data = Net::HTTP.get(uri)

    res = data.match(/var models = \[(.*)\];/)
    if !(res[1].nil?)
        puts "Code: #{key}"
        res[1].scan(/'[^']*'/).each{|model|
            puts "\t[+] #{model}"
        }
        puts "Download Link:\n\t[+] #{pkg_links[key]}"
        puts
    end
}

# instruction
puts 'INSTRUCTIONS: '
puts '1. Go to "Apple menu" -> "About this Mac" -> "More Info…" -> "Hardware"'
puts '   to see what model your mac is.'
puts '2. Match your model with the download link above and download it'
puts '3. Use 7zip application to extract the pkg file'
puts '4. Browse to "Library/Application Support/BootCamp/" in the archive'
puts '5. Inside there is a file "WindowsSupport.dmg", also use 7zip to browse inside it'
puts '6. There is "0.Apple_ISO" file, rename it to have a proper .iso extension e.g. "BootCamp.iso"'
puts '7. Mount BootCamp.iso in Windows or burn it to a CD in order to use it.'
puts 'Have fun!'



Credit:
http://blog.timvalenta.com/2011/11/19/boot-camp-driver-downloads/

No comments:

Post a Comment