-
Notifications
You must be signed in to change notification settings - Fork 1
/
flash-turingpi.ps1
227 lines (187 loc) · 6.45 KB
/
flash-turingpi.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# DESCRIPTION:
# Used to flash Turing Pi compute modules quickly
# DEPENDENCIES:
# - Raspberry Pi Imager
# - Raspberry Pi usbboot
# REQUIREMENTS:
# - Read these [docs](https://docs.turingpi.com/docs/turing-pi1-flashing-compute-modules)
# - Download a .img you want
# - Plug micro USB cable into the flash port
param (
[Parameter(Mandatory=$true)]
[String]
$Password,
$Username = "witt",
[Parameter(Mandatory = $true)]
[String]
$Hostname,
$Timezone = "America/Chicago",
[ValidateScript({Test-Path $_})]
$UserdataFilepath = "D:\user-data",
[ValidateScript({Test-Path $_})]
$RPiImagerInstallPath = "${env:ProgramFiles(x86)}\Raspberry Pi Imager",
[ValidateScript({Test-Path $_})]
$RPiBootInstallPath = "${env:ProgramFiles(x86)}\Raspberry Pi",
[ValidateScript({Test-Path $_})]
$ImagePath = "$env:USERPROFILE\Downloads\hypriotos-rpi-v1.12.3.img\hypriotos-rpi-v1.12.3.img",
$DestinationDrive = "\\.\PhysicalDrive1",
[switch]
# Beeps can be helpful for an unattended/background terminal
$Mute = $false
)
function Setup-Userdata {
$TempFilename = "$WorkingDir\temp-user-data"
$Userdata = "#cloud-config
# vim: syntax=yaml
#
# Set your hostname here, the manage_etc_hosts will update the hosts file entries as well
hostname: $Hostname
manage_etc_hosts: true
# You could modify this for your own user information
users:
- name: $Username
gecos: `"Hypriot Pirate`"
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
groups: users,docker,video,input
plain_text_passwd: $Password
lock_passwd: false
ssh_pwauth: true
chpasswd: { expire: false }
# # Set the locale of the system
# locale: `"en_US.UTF-8`"
# # Set the timezone
# # Value of 'timezone' must exist in /usr/share/zoneinfo
# timezone: $Timezone
# # Update apt packages on first boot
# package_update: true
# package_upgrade: true
# package_reboot_if_required: true
package_upgrade: false
# # Install any additional apt packages you need here
# packages:
# - ntp
# # WiFi connect to HotSpot
# # - use `wpa_passphrase SSID PASSWORD` to encrypt the psk
# write_files:
# - content: |
# allow-hotplug wlan0
# iface wlan0 inet dhcp
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# iface default inet dhcp
# path: /etc/network/interfaces.d/wlan0
# - content: |
# country=de
# ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
# update_config=1
# network={
# ssid=`"YOUR_WIFI_SSID`"
# psk=`"YOUR_WIFI_PASSWORD`"
# proto=RSN
# key_mgmt=WPA-PSK
# pairwise=CCMP
# auth_alg=OPEN
# }
# path: /etc/wpa_supplicant/wpa_supplicant.conf
# These commands will be ran once on first boot only
runcmd:
# Pickup the hostname changes
- 'systemctl restart avahi-daemon'
# # Activate WiFi interface
# - 'ifup wlan0'"
Set-Clipboard $Userdata
Write-Host "Clipboard has been set to userdata." -ForegroundColor Blue
$Userdata | Out-File $TempFilename -Force
#$Userdata | Out-File $UserdataFilepath -Force
Move-Item $TempFilename $UserdataFilepath -Force
# Test if data was transferred properly
$Success = $false
$Success = Get-ChildItem $UserdataFilepath | Select-String $Hostname
if ($Success) {
Write-Host "Userdata written successfully!" -ForegroundColor Green
} else {
Write-Host "Hostname `"$Hostname`" not found in userdata.`nManually verify the user-data file is valid before proceeding." -ForegroundColor Red
Start-Sleep -Seconds 2
Write-Host "Hit " -NoNewline
Write-Host "[Enter] " -ForegroundColor Blue -NoNewline
$null = Read-Host "when the file is in place"
$null = Read-Host "Are you sure?"
}
# Cleanup before exit
Remove-Item $TempFilename
}
function Prepare-Device {
# You should get a notification that a USB device is being setup. It should start with "BCM"
Write-Host "Waiting until device is ready..."
$Device = $null
while ($null -eq $Device) {
$Device = Get-PnpDevice -FriendlyName "BCM*" -PresentOnly -ErrorAction SilentlyContinue
if ($Device) {
Write-Host "Device found!" -ForegroundColor Green
}
else {
Start-Sleep -Seconds 5
Write-Host "Waiting until device is ready..." -ForegroundColor Blue
}
}
# Once that's done setting up, run rpiboot from elevated prompt
# Download: https://github.com/raspberrypi/usbboot/releases
# Extract zip
# Run rpiboot_setup.exe [link](https://github.com/raspberrypi/usbboot/blob/master/win32/rpiboot_setup.exe)
Write-Host "Waiting " -ForegroundColor Yellow -NoNewline
Write-Host "for rpiboot process to complete..."
& "$RPiBootInstallPath\rpiboot.exe"
# $rpibootProcess = Get-Process -Name "rpiboot" -ErrorAction SilentlyContinue
# Wait-Process -Id $rpibootProcess.Id -ErrorAction SilentlyContinue
}
function ManualSteps-Preamble {
if (!$Mute) {
[console]::beep(640, 200)
[console]::beep(840, 200)
[console]::beep(940, 200)
}
Write-Host "Seat " -ForegroundColor Yellow -NoNewline
Write-Host "compute module into TPi node # 1"
Start-Sleep -Seconds 2
Write-Host "Plug " -ForegroundColor Yellow -NoNewline
Write-Host "power into TPi"
Start-Sleep -Seconds 2
}
function ManualSteps-Postamble {
Write-Host "Remove " -ForegroundColor Yellow -NoNewline
Write-Host "TPi power cable"
Start-Sleep -Seconds 2
Write-Host "Eject " -ForegroundColor Yellow -NoNewline
Write-Host "the compute module manually"
Start-Sleep -Seconds 2
Write-Host "Re-run script if you have more modules to flash ⚡"
if (!$Mute) {
[console]::beep(940, 200)
[console]::beep(840, 200)
[console]::beep(640, 200)
}
}
function Image-Device {
Write-Host "Writing " -ForegroundColor Yellow -NoNewline
Write-Host "image to USB drive..."
Set-Location $RPiImagerInstallPath
& ".\rpi-imager-cli.cmd" --cli $ImagePath $DestinationDrive
Set-Location $WorkingDir
}
function Eject-UsbDrive {
# Eject USB drive
$DriveEject = New-Object -comObject Shell.Application
$HypriotDrive = Get-CimInstance win32_volume | Where-Object { $_.Label -eq "HypriotOS" }
$DriveEject.Namespace(17).ParseName($HypriotDrive.Name.Substring(0, 2)).InvokeVerb("Eject")
Write-Host "Drive ejected" -ForegroundColor Blue
}
# ------------- Where it runs
$WorkingDir = (Get-Location).Path
ManualSteps-Preamble
Prepare-Device
Image-Device
Start-Sleep -Seconds 3
# After write is complete, open file explorer to the USB device that's mounted named HypriotOS and customize user-data file
Setup-Userdata
Eject-UsbDrive
ManualSteps-Postamble