WinClone "Make EFI Bootable"

Look
Valued Contributor III

Hey everyone (or even the WinClone folks).
We have a few machines that when you boot into WinPE the EFI partition is not visible either when you try and mount it with mountvol S: /S or when you look for it with diskpart.
I noticed however that when you run WinClone's "Make EFI Bootable" command it then becomes visible, it's not a function of the copying of the files, but there must be some final process like a bless or something that cause this behaviour.
Does anyone have any insight into exactly what function does this as I'd love to be able to replicate it in a script.

6 REPLIES 6

franton
Valued Contributor III

Short version: it is actually copying files. The EFI boot files are copied from the Windows partition to some appropriately created folders inside the EFI partition.

Look
Valued Contributor III

Yeah I have it going as far as.
Copying into EFI the boot files and .efis etc...
Copying into EFI the BCD
Editing the BCD to include the UUID of the EFI

It's at this point it does something that results in either the EFI or the Windows partition (most likely the EFI partition) on a fresh Mac suddenly making the EFI visible and mountable as a drive if you boot to an external USB i.e. SCCM boot media.
It's the very last function I am trying to replicate

I have given up on trying to make SCCM prestages capture and deploy correctly using Winclone due to EFI only boot on new macs and the fact it defaults to normal Windows boot rather than RAM WinPE boot and just want to use that function to prep the drive instead (even a command line way of doing that using the framwork contained with a winclone image would be sufficient).

hdsst3
New Contributor
#!/bin/sh

``

```

Kumarasinghe
Valued Contributor

@Look Did you mage to capture and deploy EFI bootable SCCM Prestage media ?

Look
Valued Contributor III

@Kumarasinghe

Didn't manage to get prestaging working, what I did manage to get working was imaging out of SCCM using USB media and EFI boot.
With a bit of help from Two Canoes I worked out that you can use fdisk (some to remove the MBR data off a FAT32 volume which then makes the SCCM boot media do everything using EFI, it's get slighty complicated with Fusion drives as you get a couple of EFI partitions.

The following script will find an EMPTY FAT32 partition called exactly WINDOWS and then use fdisk to destroy it's relevant MBR data. It will also create a file called FORMATC.BAT which you call from the SCCM workflow to reformat the disk as NTFS before applying the image.
There is a small routine to hide a NONE partition as well which is part of a process of tricking a fusion machine into using the right partition.

JUST BE WARNED FDISK IS VERY DESTRUCTIVE IF USED WRONG.

#!/bin/bash

##### SUBROUTINES START HERE #####

Windows_Present_Empty() {
if [[ -d /Volumes/WINDOWS ]] && [[ ! "$(ls -1 /Volumes/WINDOWS | awk '!/^[.]/ && !/FORMATC/ && !/SMST/')" ]]; then
echo "Empty Windows partition detected"
return 0
else
echo "No WINDOWS or data on WINDOWS detected"
return 1
fi
}

##### 

Write_FORMATC() {
echo "Writing format script to WINDOWS"
echo 'FORMAT C: /FS:NTFS /Q /V:WINDOWS /y' > /Volumes/WINDOWS/FORMATC.BAT
chmod 777 /Volumes/WINDOWS/FORMATC.BAT
}

##### 

Check_MBR() {
Windows_Disk=$(diskutil list | awk '/Microsoft Basic Data/ && / WINDOWS / {print $NF}'| head -n 1 | cut -c 1-5)
if [[ ! "$(fdisk /dev/$Windows_Disk | awk '/4: 00 /')" ]]; then
echo "WINDOWS drive with MBR boot detected, attempting to correct"
(echo setpid "2"; echo "0"; echo "quit"; echo "y";) | fdisk -e /dev/$Windows_Disk
(echo setpid "3"; echo "0"; echo "quit"; echo "y";) | fdisk -e /dev/$Windows_Disk
(echo setpid "4"; echo "0"; echo "quit"; echo "y";) | fdisk -e /dev/$Windows_Disk
else
echo "No WINDOWS MBR detected"
fi
}

##### 

Hide_NONE_Drive() {
echo Hiding the NONE spacer partition
The_UUID=$(diskutil info /Volumes/NONE | awk '/Volume UUID:/ {print $NF}')
if [[ "$The_UUID" ]] && [[ "$(diskutil list | awk '/Macintosh HD/')" ]];then
echo "UUID=$The_UUID none msdos rw,noauto" > /Volumes/Macintosh HD/etc/fstab
fi
}

##### MAIN PROGRAM STARTS HERE #####

Hide_NONE_Drive
if Windows_Present_Empty; then
Write_FORMATC
Check_MBR
fi

##### FIN #####

The other parts of the puzzle are SIP that has to be disabled, you can actaully make this easier by putting refind onto the SCCM boot media with the SIP and Recovery tools enabled and set to only detect the SCCM boot itself, works quite nicely.

Then there is the formating itself I created the following script to be run as part of the imaging work flow.
ONCE AGAIN ITS DSTRUCTIVE AND REMOVES ALL PARTITIONS AS PART OF THE PROCESS
It also requires the OS X partition be named exactly Macintosh HD and you can specify the WINDOWS partition size as the first argument, leaving it blank will mean no WINDOWS partition is created.
It will rebuild a fusion drive if an SSD and HDD are present, putting WINDOWS entirely on the HDD.
It also does all the same stuff as the above EFI script if a WINDOWS partition was created.

#!/bin/bash
#Sam Look November 2015, all care and no responsibility, this sucker will blitz everything so watch out

##### SUBROUTINES START HERE #####

Get_First_HDD(){
echo Starting search for HDD
for The_Disk in $(diskutil list | awk '/internal/ && /physical/ {print $1}'); do
echo Checking $The_Disk
if [[ ! "$The_HDD" ]] && [[ "$(diskutil info $The_Disk | awk '/Solid State/ && /No/')" ]]; then
The_HDD=$The_Disk
echo HDD found at $The_HDD
break
fi
done
}

#####

Get_First_SSD(){
echo Starting search for SSD
for The_Disk in $(diskutil list | awk '/internal/ && /physical/ {print $1}'); do
echo Checking $The_Disk
if [[ ! "$The_SSD" ]] && [[ "$(diskutil info $The_Disk | awk '/Solid State/ && /Yes/')" ]]; then
The_SSD=$The_Disk
echo SSD found at $The_SSD
break
fi
done
}

#####

Remove_Existing_CS(){
CS_Group=$(diskutil cs list | awk '/-- Logical Volume Group / {print $NF}')
if [[ "$CS_Group" ]]; then
echo "Removing CS group detected at $CS_Group"
diskutil cs delete $CS_Group
sleep 2
else
echo "No CS group detected"
fi
CS_Group=$(diskutil cs list | awk '/-- Logical Volume Group / {print $NF}')
}

#####

External_Boot(){
if [[ "$(diskutil info / | awk '/Device Location/ && /Internal/')" ]]; then
return 1
else
return 0
fi
}

#####

Set_EFI_Windows(){
Windows_Disk=$(diskutil list | awk '/Microsoft Basic Data/ && /WINDOWS/ {print $NF}'| head -n 1 | cut -c 1-5)
echo $(echo $Windows_Disk)

if [[ "$Windows_Disk" ]] && [[ ! "$(fdisk /dev/$Windows_Disk | awk '/4: 00 /')" ]]; then
echo "Windows drive with MBR boot detected checking for data"
if [[ ! "$(ls -1 /Volumes/WINDOWS | awk '!/^[.]/ && !/FORMATC/  && !/SMST/')" ]]; then
echo "No data detected setting to EFI boot"
(echo setpid "2"; echo "0"; echo "quit"; echo "y";) | fdisk -e /dev/$Windows_Disk
(echo setpid "3"; echo "0"; echo "quit"; echo "y";) | fdisk -e /dev/$Windows_Disk
(echo setpid "4"; echo "0"; echo "quit"; echo "y";) | fdisk -e /dev/$Windows_Disk
echo 'FORMAT C: /FS:NTFS /Q /V:WINDOWS /y' > /Volumes/WINDOWS/FORMATC.BAT
chmod 777 /Volumes/WINDOWS/FORMATC.BAT
else
echo "Aborting because data detected on Windows"
fi
else
echo "No elligible drive detected"
fi
}

#####

Create_New_Fusion(){
if [[ $Windows_Size -gt 0 ]];then
Mac_Size=$((100 - Windows_Size))
diskutil partitionDisk $The_HDD 2 GPTFormat jhfs+ "HDD Fusion" ${Mac_Size}% fat32 "WINDOWS" R
diskutil partitionDisk $The_SSD 2 GPTFormat jhfs+ "SSD Fusion" 99% fat32 "NONE" R
sleep 2
diskutil cs create fusion $(diskutil list | awk '/SSD Fusion/ {print $NF}') $(diskutil list | awk '/HDD Fusion/ {print $NF}')
sleep 2
Set_EFI_Windows
else
diskutil partitionDisk $The_HDD 1 GPTFormat jhfs+ "HDD Fusion" 0
diskutil partitionDisk $The_SSD 1 GPTFormat jhfs+ "SSD Fusion" 0
sleep 2
diskutil cs create fusion $The_SSD $The_HDD
fi
sleep 2
CS_Group=$(diskutil cs list | awk '/-- Logical Volume Group / {print $NF}')
sleep 2
diskutil cs createVolume $CS_Group jhfs+ "Macintosh HD" 0
}

#####

Get_Windows_Size(){
if [[ $Passed_Size -gt 20 ]] && [[ $Passed_Size -lt 80 ]]; then
echo Valid Windows partition size given as $Passed_Size
Windows_Size=$Passed_Size
else
echo No windows size given
fi
}

#####
Single_Disk_Partition(){
if [[ $Windows_Size -gt 0 ]];then
Mac_Size=$((100 - Windows_Size))
diskutil partitionDisk $Single_Disk 2 GPTFormat jhfs+ "Macintosh HD" ${Mac_Size}% fat32 "WINDOWS" R
Set_EFI_Windows
else
diskutil partitionDisk $Single_Disk 1 GPTFormat jhfs+ "Macintosh HD" 0
fi
}

##### MAIN PROGRAM STARTS HERE #####

if External_Boot; then
Get_First_HDD
Get_First_SSD
Passed_Size=$(echo $4 | tr -cd [:digit:])
Get_Windows_Size

    if [[ "$The_SSD" ]] || [[ "$The_HDD" ]]; then
    Remove_Existing_CS
    fi

    if [[ "$The_SSD" ]] && [[ "$The_HDD" ]]; then
    echo "Drives required for fusion detected"

        if [[ ! "$CS_Group" ]]; then
        Create_New_Fusion
        else
        echo "Cannot create fusion as previous CS group cannot be removed"
        fi

    elif [[ "$The_SSD" ]]; then
    Single_Disk=$The_SSD
    Single_Disk_Partition
    elif [[ "$The_HDD" ]]; then
    Single_Disk=$The_HDD
    Single_Disk_Partition
    fi

else
echo "This script cannot be run while booted off the internal drive"
fi

##### FIN #####

Look
Valued Contributor III

No idea how this will go with Windows 7, but it deals with Windows 10 pretty well.