|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows.Forms; |
| 11 | + |
| 12 | +namespace esp_tools_gui |
| 13 | +{ |
| 14 | + public partial class PartTool : Form |
| 15 | + { |
| 16 | + static int flashSizeBytes = 0; |
| 17 | + static int flashSizeKB = 0; |
| 18 | + |
| 19 | + private ToolPartition partition; |
| 20 | + |
| 21 | + public static int BaseAddress = 0x9000; |
| 22 | + |
| 23 | + public static int Eeprom = 0; |
| 24 | + public static int Nvs = 0; |
| 25 | + public static int Ota0 = 0; |
| 26 | + public static int Ota1 = 0; |
| 27 | + public static int Otad = 8 * 1024; |
| 28 | + public static bool OtaEnabled = false; |
| 29 | + public static int Spiffs = 0; |
| 30 | + |
| 31 | + public PartTool(int flashMB, ToolPartition partitionTool) |
| 32 | + { |
| 33 | + InitializeComponent(); |
| 34 | + flashSizeBytes = flashMB * 1024 * 1024; |
| 35 | + flashSizeKB = flashMB * 1024; |
| 36 | + |
| 37 | + progressBar1.Value = 0; |
| 38 | + progressBar1.Maximum = flashSizeKB; |
| 39 | + |
| 40 | + partition = partitionTool; |
| 41 | + } |
| 42 | + |
| 43 | + private void PartTool_Load(object sender, EventArgs e) |
| 44 | + { |
| 45 | + trackBarOta.SmallChange = 64; |
| 46 | + trackBarOta1.SmallChange = 64; |
| 47 | + trackBarOta.TickFrequency = 256; |
| 48 | + trackBarOta1.TickFrequency = 256; |
| 49 | + |
| 50 | + trackBarEeprom.SmallChange = 4; |
| 51 | + trackBarNvs.SmallChange = 4; |
| 52 | + trackBarEeprom.TickFrequency = 4; |
| 53 | + trackBarNvs.TickFrequency = 4; |
| 54 | + |
| 55 | + trackBarSpiffs.SmallChange = 4; |
| 56 | + trackBarSpiffs.TickFrequency = 128; |
| 57 | + Calculate(); |
| 58 | + } |
| 59 | + |
| 60 | + public int Calculate() |
| 61 | + { |
| 62 | + int total = 0; |
| 63 | + int free = 0; |
| 64 | + total = BaseAddress; |
| 65 | + if (checkBoxNvs.Checked) total += Nvs; |
| 66 | + if (checkBoxOta.Checked) total += Otad + Ota0 + Ota1; |
| 67 | + else total += Ota0; |
| 68 | + if (checkBoxEeprom.Checked) total += Eeprom; |
| 69 | + if (checkBoxSpiffs.Checked) total += Spiffs; |
| 70 | + total /= 1024; |
| 71 | + free = flashSizeKB - total; |
| 72 | + |
| 73 | + progressBar1.Value = Math.Min(progressBar1.Maximum, total); |
| 74 | + if(total > progressBar1.Maximum) |
| 75 | + { |
| 76 | + labelProgress.ForeColor = Color.DarkRed; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + labelProgress.ForeColor = Color.Black; |
| 81 | + } |
| 82 | + labelProgress.Text = total + "kb / " + flashSizeKB + "kb"; |
| 83 | + |
| 84 | + labelOta.Text = trackBarOta.Value + "kb"; |
| 85 | + if (checkBoxOtaLock.Checked) |
| 86 | + { |
| 87 | + labelOta1.Text = trackBarOta.Value + "kb"; |
| 88 | + trackBarOta1.Value = trackBarOta.Value; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + labelOta1.Text = trackBarOta1.Value + "kb"; |
| 93 | + } |
| 94 | + labelNvs.Text = (checkBoxNvs.Checked ? trackBarNvs.Value.ToString() : "0") + "kb"; |
| 95 | + labelEeprom.Text = (checkBoxEeprom.Checked ? trackBarEeprom.Value.ToString() : "0") + "kb"; |
| 96 | + labelSpiffs.Text = (checkBoxSpiffs.Checked ? trackBarSpiffs.Value.ToString() : "0") + "kb"; |
| 97 | + |
| 98 | + if (free < 0) return free; |
| 99 | + |
| 100 | + trackBarEeprom.Maximum = trackBarEeprom.Value + free; |
| 101 | + trackBarNvs.Maximum = trackBarNvs.Value + free; |
| 102 | + if (checkBoxOta.Checked) |
| 103 | + trackBarOta.Maximum = trackBarOta.Value + free / 2; |
| 104 | + else |
| 105 | + trackBarOta.Maximum = trackBarOta.Value + free; |
| 106 | + trackBarOta1.Maximum = trackBarOta1.Value + free; |
| 107 | + trackBarSpiffs.Maximum = trackBarSpiffs.Value + free; |
| 108 | + |
| 109 | + return free; |
| 110 | + } |
| 111 | + |
| 112 | + public void SetEeprom(int size) |
| 113 | + { |
| 114 | + Eeprom = size; |
| 115 | + if (size / 1024 > trackBarEeprom.Maximum) trackBarEeprom.Maximum = size / 1024; |
| 116 | + trackBarEeprom.Value = size / 1024; |
| 117 | + Calculate(); |
| 118 | + } |
| 119 | + public void SetNvs(int size) |
| 120 | + { |
| 121 | + Nvs = size; |
| 122 | + if (size / 1024 > trackBarNvs.Maximum) trackBarNvs.Maximum = size / 1024; |
| 123 | + trackBarNvs.Value = size / 1024; |
| 124 | + Calculate(); |
| 125 | + } |
| 126 | + public void SetOta0(int size, bool? useOta) |
| 127 | + { |
| 128 | + Ota0 = size; |
| 129 | + if (size / 1024 > trackBarOta.Maximum) trackBarOta.Maximum = size / 1024; |
| 130 | + trackBarOta.Value = size / 1024; |
| 131 | + if (useOta != null) |
| 132 | + { |
| 133 | + OtaEnabled = (bool)useOta; |
| 134 | + checkBoxOta.Checked = (bool)useOta; |
| 135 | + if (useOta == true) |
| 136 | + { |
| 137 | + Ota1 = size; |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + Ota1 = 0; |
| 142 | + } |
| 143 | + } |
| 144 | + Calculate(); |
| 145 | + } |
| 146 | + public void SetOta1(int size) |
| 147 | + { |
| 148 | + Ota1 = size; |
| 149 | + if (size / 1024 > trackBarOta1.Maximum) trackBarOta1.Maximum = size / 1024; |
| 150 | + trackBarOta1.Value = size / 1024; |
| 151 | + checkBoxOta.Checked = true; |
| 152 | + checkBoxOtaLock.Checked = (Ota1 == Ota0); |
| 153 | + OtaEnabled = true; |
| 154 | + Calculate(); |
| 155 | + } |
| 156 | + |
| 157 | + public void SetSpiffs(int size) |
| 158 | + { |
| 159 | + Spiffs = size; |
| 160 | + if (size / 1024 > trackBarSpiffs.Maximum) trackBarSpiffs.Maximum = size / 1024; |
| 161 | + trackBarSpiffs.Value = size / 1024; |
| 162 | + Calculate(); |
| 163 | + } |
| 164 | + |
| 165 | + private void trackBarOta_ValueChanged(object sender, EventArgs e) |
| 166 | + { |
| 167 | + Ota0 = (trackBarOta.Value - trackBarOta.Value % 64) * 1024; |
| 168 | + trackBarOta.Value = Ota0 / 1024; |
| 169 | + if (checkBoxOtaLock.Checked) |
| 170 | + { |
| 171 | + Ota1 = Ota0; |
| 172 | + if (Ota0 / 1024 > trackBarOta1.Maximum) trackBarOta1.Maximum = Ota0 / 1024; |
| 173 | + trackBarOta1.Value = Ota0 / 1024; |
| 174 | + if (trackBarOta.Value > trackBarOta1.Maximum) trackBarOta1.Maximum = trackBarOta.Value; |
| 175 | + trackBarOta1.Value = trackBarOta.Value; |
| 176 | + } |
| 177 | + Calculate(); |
| 178 | + } |
| 179 | + |
| 180 | + private void trackBarOta1_ValueChanged(object sender, EventArgs e) |
| 181 | + { |
| 182 | + if (!checkBoxOtaLock.Checked) |
| 183 | + { |
| 184 | + Ota1 = (trackBarOta1.Value - (trackBarOta1.Value % 64)) * 1024; |
| 185 | + trackBarOta1.Value = Ota1 / 1024; |
| 186 | + Calculate(); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private void trackBarNvs_ValueChanged(object sender, EventArgs e) |
| 191 | + { |
| 192 | + Nvs = (trackBarNvs.Value - trackBarNvs.Value % 4) * 1024; |
| 193 | + trackBarNvs.Value = Nvs / 1024; |
| 194 | + Calculate(); |
| 195 | + } |
| 196 | + |
| 197 | + private void trackBarEeprom_ValueChanged(object sender, EventArgs e) |
| 198 | + { |
| 199 | + Eeprom = (trackBarEeprom.Value - trackBarEeprom.Value % 4) * 1024; |
| 200 | + trackBarEeprom.Value = Eeprom / 1024; |
| 201 | + Calculate(); |
| 202 | + } |
| 203 | + |
| 204 | + private void trackBarSpiffs_ValueChanged(object sender, EventArgs e) |
| 205 | + { |
| 206 | + Spiffs = (trackBarSpiffs.Value - trackBarSpiffs.Value % 4) * 1024; |
| 207 | + trackBarSpiffs.Value = Spiffs / 1024; |
| 208 | + Calculate(); |
| 209 | + } |
| 210 | + |
| 211 | + private void checkBoxOta_CheckedChanged(object sender, EventArgs e) |
| 212 | + { |
| 213 | + OtaEnabled = checkBoxOta.Checked; |
| 214 | + checkBoxOtaLock.Enabled = checkBoxOta.Checked; |
| 215 | + if (checkBoxOta.Checked) |
| 216 | + { |
| 217 | + trackBarOta1.Enabled = !checkBoxOtaLock.Checked; |
| 218 | + Ota1 = Ota0; |
| 219 | + Calculate(); |
| 220 | + } |
| 221 | + else |
| 222 | + { |
| 223 | + trackBarOta1.Enabled = false; |
| 224 | + Ota1 = 0; |
| 225 | + Calculate(); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + private void checkBoxNvs_CheckedChanged(object sender, EventArgs e) |
| 230 | + { |
| 231 | + if (checkBoxNvs.Checked) |
| 232 | + { |
| 233 | + Nvs = trackBarNvs.Value * 1024; |
| 234 | + trackBarNvs.Enabled = true; |
| 235 | + Calculate(); |
| 236 | + } |
| 237 | + else |
| 238 | + { |
| 239 | + Nvs = 0; |
| 240 | + trackBarNvs.Enabled = false; |
| 241 | + Calculate(); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + private void checkBoxEeprom_CheckedChanged(object sender, EventArgs e) |
| 246 | + { |
| 247 | + if (checkBoxEeprom.Checked) |
| 248 | + { |
| 249 | + Eeprom = trackBarEeprom.Value * 1024; |
| 250 | + trackBarEeprom.Enabled = true; |
| 251 | + Calculate(); |
| 252 | + } |
| 253 | + else |
| 254 | + { |
| 255 | + Eeprom = 0; |
| 256 | + trackBarEeprom.Enabled = false; |
| 257 | + Calculate(); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + private void checkBoxSpiffs_CheckedChanged(object sender, EventArgs e) |
| 262 | + { |
| 263 | + if (checkBoxSpiffs.Checked) |
| 264 | + { |
| 265 | + Spiffs = trackBarSpiffs.Value * 1024; |
| 266 | + trackBarSpiffs.Enabled = true; |
| 267 | + Calculate(); |
| 268 | + } |
| 269 | + else |
| 270 | + { |
| 271 | + Spiffs = 0; |
| 272 | + trackBarSpiffs.Enabled = false; |
| 273 | + Calculate(); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + private void button3_Click(object sender, EventArgs e) |
| 278 | + { |
| 279 | + DialogResult = DialogResult.Cancel; |
| 280 | + this.Close(); |
| 281 | + } |
| 282 | + |
| 283 | + private void button2_Click(object sender, EventArgs e) |
| 284 | + { |
| 285 | + DialogResult = DialogResult.Yes; |
| 286 | + Close(); |
| 287 | + } |
| 288 | + |
| 289 | + private void trackBarOta_Scroll(object sender, EventArgs e) |
| 290 | + { |
| 291 | + |
| 292 | + } |
| 293 | + |
| 294 | + private void button1_Click(object sender, EventArgs e) |
| 295 | + { |
| 296 | + if (saveFileDialog1.ShowDialog() == DialogResult.OK) |
| 297 | + { |
| 298 | + SavePartition(saveFileDialog1.FileName); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + private async void SavePartition(string filename) |
| 303 | + { |
| 304 | + await partition.CreatePartition(Nvs, Ota0, Ota1, Eeprom, Spiffs); |
| 305 | + File.Copy(partition.GetPartitionPath(filename.EndsWith(".bin")), filename); |
| 306 | + } |
| 307 | + |
| 308 | + private void checkBoxOtaLock_CheckedChanged(object sender, EventArgs e) |
| 309 | + { |
| 310 | + trackBarOta1.Enabled = !checkBoxOtaLock.Checked; |
| 311 | + } |
| 312 | + |
| 313 | + private async void button4_Click(object sender, EventArgs e) |
| 314 | + { |
| 315 | + if (openFileDialog1.ShowDialog() == DialogResult.OK) |
| 316 | + { |
| 317 | + if(await partition.ImportTable(openFileDialog1.FileName)) |
| 318 | + { |
| 319 | + var csv = File.ReadAllLines(partition.GetPartitionPath(false)); |
| 320 | + foreach(var line in csv) |
| 321 | + { |
| 322 | + if (line.StartsWith("#")) continue; |
| 323 | + var items = line.Split(','); |
| 324 | + if (items.Count() < 5) continue; |
| 325 | + switch(items[0].Trim()) |
| 326 | + { |
| 327 | + case "nvs": SetNvs(Convert.ToInt32(items[4].Trim(), 16)); break; |
| 328 | + case "app0": SetOta0(Convert.ToInt32(items[4].Trim(), 16), null); break; |
| 329 | + case "app1": SetOta1(Convert.ToInt32(items[4].Trim(), 16)); break; |
| 330 | + case "eeprom": SetEeprom(Convert.ToInt32(items[4].Trim(), 16)); break; |
| 331 | + case "spiffs": SetSpiffs(Convert.ToInt32(items[4].Trim(), 16)); break; |
| 332 | + case "otadata": Otad = Convert.ToInt32(items[4].Trim(), 16); break; |
| 333 | + default: MessageBox.Show("Error: can't import this partition: " + items[0].Trim() + " - it will be ignored", "Import partitions"); break; |
| 334 | + } |
| 335 | + |
| 336 | + /* |
| 337 | + nvs, data, nvs, 0x9000, 0x5000, |
| 338 | + otadata, data, ota, 0xe000, 0x2000, |
| 339 | + app0, app, ota_0, 0x10000, 0x180000, |
| 340 | + app1, app, ota_1, 0x190000, 0x100000, |
| 341 | + eeprom, data, 0x99, 0x310000, 0x1000, |
| 342 | + spiffs, data, spiffs, 0x311000, 0x16f000, |
| 343 | + */ |
| 344 | + } |
| 345 | + } |
| 346 | + else |
| 347 | + { |
| 348 | + MessageBox.Show("Unable to import partition table " + openFileDialog1.FileName, "Import"); |
| 349 | + } |
| 350 | + } |
| 351 | + } |
| 352 | + } |
| 353 | +} |
0 commit comments