Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleanup the webupdate code

  * Store files on ramdisk
  * merge download and flash task to one
  * MTK is not thread safe, call 'Messagebox' only from the GUI task.
    move the display_flashvalid_message() into update_progress().
  * Use the same ok_callback with different argument.
  • Loading branch information...
commit 3ca7f6534ec392f493fcae3e6069f8a8ebe75f55 1 parent c2a7a49
authored May 19, 2011

Showing 1 changed file with 143 additions and 178 deletions. Show diff stats Hide diff stats

  1. 321  src/flash.c
321  src/flash.c
@@ -44,6 +44,11 @@ static struct filedialog *file_dlg;
44 44
 static int current_file_to_choose;
45 45
 
46 46
 enum {
  47
+	ARG_FILE_UPDATE = 0,
  48
+	ARG_WEB_UPDATE
  49
+};
  50
+
  51
+enum {
47 52
 	FLASH_STATE_READY = 0,
48 53
 	FLASH_STATE_STARTING,
49 54
 
@@ -73,11 +78,54 @@ enum {
73 78
 
74 79
 static int flash_state;
75 80
 static int flash_progress;
  81
+static int flashvalid_val;
76 82
 
77 83
 static char bitstream_name[384];
78 84
 static char bios_name[384];
79 85
 static char application_name[384];
80 86
 
  87
+static double *d_progress;
  88
+static int progress_callback(void *d_progress, double t, double d, double ultotal, double ulnow)
  89
+{
  90
+	flash_progress = (int) d/t*100;
  91
+	return 0;
  92
+}
  93
+
  94
+static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  95
+	size_t written;
  96
+	written = fwrite(ptr, size, nmemb, stream);
  97
+	return written;
  98
+}
  99
+
  100
+static int download(const char *url, const char *filename)
  101
+{
  102
+	CURL *curl;
  103
+	FILE *fp;
  104
+	int ret = 0;
  105
+
  106
+	fp = fopen(filename, "wb");
  107
+	if(fp == NULL) return 0;
  108
+
  109
+	curl = curl_easy_init();
  110
+	if (curl) {
  111
+		curl_easy_setopt(curl, CURLOPT_URL, url);
  112
+		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  113
+		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  114
+
  115
+		curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  116
+		curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  117
+		curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &d_progress);
  118
+
  119
+		if(curl_easy_perform(curl) == 0)
  120
+			ret = 1;
  121
+
  122
+		curl_easy_cleanup(curl);
  123
+	}
  124
+
  125
+	fclose(fp);
  126
+	return ret;
  127
+}
  128
+
81 129
 static int flash_erase(int fd, unsigned int len)
82 130
 {
83 131
 	int r;
@@ -196,18 +244,64 @@ static void flash_terminate(int state)
196 244
 
197 245
 static rtems_task flash_task(rtems_task_argument argument)
198 246
 {
199  
-	if(bitstream_name[0] != 0) {
200  
-		if(!flash_file("/dev/flash1", bitstream_name, FLASH_STATE_ERASE_BITSTREAM, FLASH_STATE_PROGRAM_BITSTREAM))
201  
-			flash_terminate(FLASH_STATE_ERROR_BITSTREAM);
202  
-	}
203  
-	if(bios_name[0] != 0) {
204  
-		if(!flash_file("/dev/flash2", bios_name, FLASH_STATE_ERASE_BIOS, FLASH_STATE_PROGRAM_BIOS))
205  
-			flash_terminate(FLASH_STATE_ERROR_BIOS);
206  
-	}
207  
-	if(application_name[0] != 0) {
208  
-		if(!flash_file("/dev/flash4", application_name, FLASH_STATE_ERASE_APP, FLASH_STATE_PROGRAM_APP))
209  
-			flash_terminate(FLASH_STATE_ERROR_APP);
  247
+	const char *s_url = "http://www.milkymist.org/snapshots/latest/soc.fpg";
  248
+	const char *b_url = "http://www.milkymist.org/snapshots/latest/bios.bin";
  249
+	const char *f_url = "http://www.milkymist.org/snapshots/latest/flickernoise.fbi";
  250
+	const char *s = "/ramdisk/soc.fpg";
  251
+	const char *b = "/ramdisk/bios.bin";
  252
+	const char *f = "/ramdisk/flickernoise.fbi";
  253
+
  254
+	int task = (int)argument;
  255
+
  256
+	switch (task) {
  257
+	case ARG_WEB_UPDATE:
  258
+		flash_state = DOWNLOAD_STATE_START_BITSTREAM;
  259
+		if(!download(s_url, s))
  260
+			flash_terminate(DOWNLOAD_STATE_ERROR_BITSTREAM);
  261
+		mtk_cmdf(appid, "e1.set(-text \"%s\")", s);
  262
+		strcpy(bitstream_name, s);
  263
+
  264
+		flash_state = DOWNLOAD_STATE_START_BIOS;
  265
+		if(!download(b_url, b))
  266
+			flash_terminate(DOWNLOAD_STATE_ERROR_BIOS);
  267
+		mtk_cmdf(appid, "e2.set(-text \"%s\")", b);
  268
+		strcpy(bios_name, b);
  269
+
  270
+		flash_state = DOWNLOAD_STATE_START_APP;
  271
+		if(!download(f_url, f))
  272
+			flash_terminate(DOWNLOAD_STATE_ERROR_APP);
  273
+		mtk_cmdf(appid, "e3.set(-text \"%s\")", f);
  274
+		strcpy(application_name, f);
  275
+	case ARG_FILE_UPDATE:
  276
+		if(bitstream_name[0] != 0) {
  277
+			flashvalid_val = flashvalid_bitstream(bitstream_name);
  278
+			if(flashvalid_val != FLASHVALID_PASSED)
  279
+				flash_terminate(FLASH_STATE_ERROR_BITSTREAM);
  280
+
  281
+			if(!flash_file("/dev/flash1", bitstream_name,
  282
+				       FLASH_STATE_ERASE_BITSTREAM, FLASH_STATE_PROGRAM_BITSTREAM))
  283
+				flash_terminate(FLASH_STATE_ERROR_BITSTREAM);
  284
+		}
  285
+		if(bios_name[0] != 0) {
  286
+			flashvalid_val = flashvalid_bios(bios_name);
  287
+			if(flashvalid_val != FLASHVALID_PASSED)
  288
+				flash_terminate(FLASH_STATE_ERROR_BIOS);
  289
+
  290
+			if(!flash_file("/dev/flash2", bios_name,
  291
+				       FLASH_STATE_ERASE_BIOS, FLASH_STATE_PROGRAM_BIOS))
  292
+				flash_terminate(FLASH_STATE_ERROR_BIOS);
  293
+		}
  294
+		if(application_name[0] != 0) {
  295
+			flashvalid_val = flashvalid_application(application_name);
  296
+			if(flashvalid_val != FLASHVALID_PASSED)
  297
+				flash_terminate(FLASH_STATE_ERROR_APP);
  298
+
  299
+			if(!flash_file("/dev/flash4", application_name,
  300
+				       FLASH_STATE_ERASE_APP, FLASH_STATE_PROGRAM_APP))
  301
+				flash_terminate(FLASH_STATE_ERROR_APP);
  302
+		}
210 303
 	}
  304
+
211 305
 	flash_terminate(FLASH_STATE_SUCCESS);
212 306
 }
213 307
 
@@ -244,6 +338,19 @@ static void refresh_callback(mtk_event *e, int count)
244 338
 	}
245 339
 }
246 340
 
  341
+static void display_flashvalid_message(const char *n)
  342
+{
  343
+	char buf[256];
  344
+
  345
+	if(flashvalid_val == FLASHVALID_ERROR_IO) {
  346
+		sprintf(buf, "Unable to read the %s image.\nCheck that the file exists. Operation aborted.", n);
  347
+		messagebox("I/O error", buf);
  348
+	} else if(flashvalid_val == FLASHVALID_ERROR_FORMAT) {
  349
+		sprintf(buf, "The format of the %s image is not recognized.\nHave you selected the correct file? Operation aborted.", n);
  350
+		messagebox("Format error", buf);
  351
+	}
  352
+}
  353
+
247 354
 static void update_progress()
248 355
 {
249 356
 	switch(flash_state) {
@@ -261,6 +368,7 @@ static void update_progress()
261 368
 			break;
262 369
 		case FLASH_STATE_ERROR_BITSTREAM:
263 370
 			mtk_cmd(appid, "l_stat.set(-text \"Failed to program bitstream.\")");
  371
+			display_flashvalid_message("bitstream");
264 372
 			input_delete_callback(refresh_callback);
265 373
 			break;
266 374
 		case FLASH_STATE_ERASE_BIOS:
@@ -271,6 +379,7 @@ static void update_progress()
271 379
 			break;
272 380
 		case FLASH_STATE_ERROR_BIOS:
273 381
 			mtk_cmd(appid, "l_stat.set(-text \"Failed to program BIOS.\")");
  382
+			display_flashvalid_message("BIOS");
274 383
 			input_delete_callback(refresh_callback);
275 384
 			break;
276 385
 		case FLASH_STATE_ERASE_APP:
@@ -281,6 +390,7 @@ static void update_progress()
281 390
 			break;
282 391
 		case FLASH_STATE_ERROR_APP:
283 392
 			mtk_cmd(appid, "l_stat.set(-text \"Failed to program application.\")");
  393
+			display_flashvalid_message("application");
284 394
 			input_delete_callback(refresh_callback);
285 395
 			break;
286 396
 		case DOWNLOAD_STATE_START_BITSTREAM:
@@ -291,7 +401,7 @@ static void update_progress()
291 401
 			input_delete_callback(refresh_callback);
292 402
 			break;
293 403
 		case DOWNLOAD_STATE_START_BIOS:
294  
-			mtk_cmd(appid, "l_stat.set(-text \"Downloading bios...\")");
  404
+			mtk_cmd(appid, "l_stat.set(-text \"Downloading BIOS...\")");
295 405
 			break;
296 406
 		case DOWNLOAD_STATE_ERROR_BIOS:
297 407
 			mtk_cmd(appid, "l_stat.set(-text \"Failed to download bitstream.\")");
@@ -314,68 +424,37 @@ static void update_progress()
314 424
 
315 425
 static rtems_id flash_task_id;
316 426
 
317  
-static void display_flashvalid_message(int val, const char *n)
318  
-{
319  
-	char buf[256];
320  
-	
321  
-	if(val == FLASHVALID_ERROR_IO) {
322  
-		sprintf(buf, "Unable to read the %s image.\nCheck that the file exists. Operation aborted.", n);
323  
-		messagebox("I/O error", buf);
324  
-	} else if(val == FLASHVALID_ERROR_FORMAT) {
325  
-		sprintf(buf, "The format of the %s image is not recognized.\nHave you selected the correct file? Operation aborted.", n);
326  
-		messagebox("Format error", buf);
327  
-	}
328  
-}
329  
-
330 427
 static void ok_callback(mtk_event *e, void *arg)
331 428
 {
332 429
 	rtems_status_code sc;
333  
-	int nothing;
334  
-	int val;
335 430
 	
336 431
 	if(flash_busy()) return;
337 432
 
338  
-	mtk_req(appid, bitstream_name, sizeof(bitstream_name), "e1.text");
339  
-	mtk_req(appid, bios_name, sizeof(bios_name), "e2.text");
340  
-	mtk_req(appid, application_name, sizeof(application_name), "e3.text");
341  
-
342  
-	/* Sanity checks */
343  
-	nothing = 1;
344  
-	if(bitstream_name[0] != 0) {
345  
-		nothing = 0;
346  
-		val = flashvalid_bitstream(bitstream_name);
347  
-		display_flashvalid_message(val, "bitstream");
348  
-		if(val != FLASHVALID_PASSED) return;
349  
-	}
350  
-	if(bios_name[0] != 0) {
351  
-		nothing = 0;
352  
-		val = flashvalid_bios(bios_name);
353  
-		display_flashvalid_message(val, "BIOS");
354  
-		if(val != FLASHVALID_PASSED) return;
355  
-	}
356  
-	if(application_name[0] != 0) {
357  
-		nothing = 0;
358  
-		val = flashvalid_application(application_name);
359  
-		display_flashvalid_message(val, "application");
360  
-		if(val != FLASHVALID_PASSED) return;
361  
-	}
362  
-	if(nothing) {
363  
-		messagebox("Nothing to do!", "No flash images are specified.");
364  
-		return;
  433
+	if((int)arg == ARG_FILE_UPDATE) {
  434
+		mtk_req(appid, bitstream_name, sizeof(bitstream_name), "e1.text");
  435
+		mtk_req(appid, bios_name, sizeof(bios_name), "e2.text");
  436
+		mtk_req(appid, application_name, sizeof(application_name), "e3.text");
  437
+
  438
+		/* Sanity checks */
  439
+		if((bitstream_name[0] == 0) && (bios_name[0] == 0) &&(application_name[0] == 0)) {
  440
+			messagebox("Nothing to do!", "No flash images are specified.");
  441
+			return;
  442
+		}
  443
+		/* prevent a race condition that could cause two flash tasks to start
  444
+		 * if this function is called twice quickly.
  445
+		 */
  446
+		flash_state = FLASH_STATE_STARTING;
365 447
 	}
366 448
 
367  
-	/* prevent a race condition that could cause two flash tasks to start
368  
-	 * if this function is called twice quickly.
369  
-	 */
370  
-	flash_state = FLASH_STATE_STARTING;
371 449
 	flash_progress = 0;
372  
-	
  450
+
373 451
 	/* start flashing in the background */
374 452
 	sc = rtems_task_create(rtems_build_name('F', 'L', 'S', 'H'), 100, 24*1024,
375  
-		RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR,
376  
-		0, &flash_task_id);
  453
+			       RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR,
  454
+			       0, &flash_task_id);
377 455
 	assert(sc == RTEMS_SUCCESSFUL);
378  
-	sc = rtems_task_start(flash_task_id, flash_task, 0);
  456
+
  457
+	sc = rtems_task_start(flash_task_id, flash_task, (int)arg);
379 458
 	assert(sc == RTEMS_SUCCESSFUL);
380 459
 
381 460
 	/* display update */
@@ -393,120 +472,6 @@ static void close_callback(mtk_event *e, void *arg)
393 472
 	w_open = 0;
394 473
 }
395 474
 
396  
-static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
397  
-	size_t written;
398  
-	written = fwrite(ptr, size, nmemb, stream);
399  
-	return written;
400  
-}
401  
-
402  
-static double *d_progress;
403  
-
404  
-int progress_callback(void *d_progress, double t, double d, double ultotal, double ulnow)
405  
-{
406  
-	flash_progress = (int) d/t*100;
407  
-	return 0;
408  
-}
409  
-
410  
-static int download(char *url, char *filename)
411  
-{
412  
-	CURL *curl;
413  
-	FILE *fp;
414  
-	int ret = 0;
415  
-
416  
-	fp = fopen(filename, "wb");
417  
-	if(fp == NULL) return 0;
418  
-
419  
-	curl = curl_easy_init();
420  
-	if (curl) {
421  
-		curl_easy_setopt(curl, CURLOPT_URL, url);
422  
-		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
423  
-		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
424  
-
425  
-		curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
426  
-		curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
427  
-		curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &d_progress);
428  
-
429  
-		if(curl_easy_perform(curl) == 0)
430  
-			ret = 1;
431  
-
432  
-		curl_easy_cleanup(curl);
433  
-	}
434  
-
435  
-	fclose(fp);
436  
-	return ret;
437  
-}
438  
-
439  
-static rtems_id download_task_id;
440  
-
441  
-static rtems_task download_task(rtems_task_argument argument)
442  
-{
443  
-	int val;
444  
-	char *s_url = "http://www.milkymist.org/snapshots/latest/soc.fpg";
445  
-	char *b_url = "http://www.milkymist.org/snapshots/latest/bios.bin";
446  
-	char *f_url = "http://www.milkymist.org/snapshots/latest/flickernoise.fbi";
447  
-
448  
-	char *s = "/flash/soc.fpg";
449  
-	char *b = "/flash/bios.bin";
450  
-	char *f = "/flash/flickernoise.fbi";
451  
-
452  
-	flash_state = DOWNLOAD_STATE_START_BITSTREAM;
453  
-	if(!download(s_url, s))
454  
-		flash_terminate(DOWNLOAD_STATE_ERROR_BITSTREAM);
455  
-
456  
-	val = flashvalid_bitstream(s);
457  
-	display_flashvalid_message(val, "bitstream");
458  
-	if(val != FLASHVALID_PASSED)
459  
-		flash_terminate(DOWNLOAD_STATE_ERROR_BITSTREAM);
460  
-	mtk_cmdf(appid, "e1.set(-text \"%s\")", s);
461  
-
462  
-	flash_state = DOWNLOAD_STATE_START_BIOS;
463  
-	if(!download(b_url, b))
464  
-		flash_terminate(DOWNLOAD_STATE_ERROR_BIOS);
465  
-
466  
-	val = flashvalid_bios(b);
467  
-	display_flashvalid_message(val, "BIOS");
468  
-	if(val != FLASHVALID_PASSED)
469  
-		flash_terminate(DOWNLOAD_STATE_ERROR_BIOS);
470  
-	mtk_cmdf(appid, "e2.set(-text \"%s\")", b);
471  
-
472  
-	flash_state = DOWNLOAD_STATE_START_APP;
473  
-	if(!download(f_url, f))
474  
-		flash_terminate(DOWNLOAD_STATE_ERROR_APP);
475  
-
476  
-	val = flashvalid_application(f);
477  
-	display_flashvalid_message(val, "application");
478  
-	if(val != FLASHVALID_PASSED)
479  
-		flash_terminate(DOWNLOAD_STATE_ERROR_APP);
480  
-	mtk_cmdf(appid, "e3.set(-text \"%s\")", f);
481  
-
482  
-	if(!flash_file("/dev/flash1", s, FLASH_STATE_ERASE_BITSTREAM, FLASH_STATE_PROGRAM_BITSTREAM))
483  
-		flash_terminate(FLASH_STATE_ERROR_BITSTREAM);
484  
-	if(!flash_file("/dev/flash2", b, FLASH_STATE_ERASE_BIOS, FLASH_STATE_PROGRAM_BIOS))
485  
-		flash_terminate(FLASH_STATE_ERROR_BIOS);
486  
-	if(!flash_file("/dev/flash4", f, FLASH_STATE_ERASE_APP, FLASH_STATE_PROGRAM_APP))
487  
-		flash_terminate(FLASH_STATE_ERROR_APP);
488  
-
489  
-	flash_terminate(FLASH_STATE_SUCCESS);
490  
-}
491  
-
492  
-static void webupdate_callback(mtk_event *e, void *arg)
493  
-{
494  
-	rtems_status_code sc;
495  
-
496  
-	if(flash_busy()) return;
497  
-
498  
-	sc = rtems_task_create(rtems_build_name('D', 'O', 'W', 'N'), 100, 24*1024,
499  
-		RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR,
500  
-		0, &download_task_id);
501  
-	assert(sc == RTEMS_SUCCESSFUL);
502  
-	sc = rtems_task_start(download_task_id, download_task, 0);
503  
-	assert(sc == RTEMS_SUCCESSFUL);
504  
-
505  
-	/* display update */
506  
-	next_update = rtems_clock_get_ticks_since_boot() + UPDATE_PERIOD;
507  
-	input_add_callback(refresh_callback);
508  
-}
509  
-
510 475
 static void flash_filedialog_ok_callback()
511 476
 {
512 477
 	char filepath[384];
@@ -598,8 +563,8 @@ void init_flash()
598 563
 	mtk_bind(appid, "b_browse2", "commit", opendialog_callback, (void *)2);
599 564
 	mtk_bind(appid, "b_browse3", "commit", opendialog_callback, (void *)3);
600 565
 	mtk_bind(appid, "b_close", "commit", close_callback, NULL);
601  
-	mtk_bind(appid, "b_ok", "commit", ok_callback, NULL);
602  
-	mtk_bind(appid, "b_webupdate", "commit", webupdate_callback, NULL);
  566
+	mtk_bind(appid, "b_ok", "commit", ok_callback, (void *)ARG_FILE_UPDATE);
  567
+	mtk_bind(appid, "b_webupdate", "commit", ok_callback, (void *)ARG_WEB_UPDATE);
603 568
 
604 569
 	mtk_bind(appid, "w", "close", close_callback, NULL);
605 570
 

0 notes on commit 3ca7f65

Please sign in to comment.
Something went wrong with that request. Please try again.