1 #ifdef ENABLE_FLASHTOOL
2 /**********************************************
6 **********************************************/
11 #include <lib/gui/emessage.h>
12 #include <lib/gui/eprogress.h>
13 #include <lib/dvb/decoder.h>
16 #include <sys/reboot.h>
22 #include <sys/ioctl.h>
23 #include <sys/mount.h>
24 #include <linux/version.h>
25 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,7)) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
26 #include <linux/compiler.h>
28 #include <mtd/mtd-user.h>
30 class FlashProgressWindow
: public eWindow
34 FlashProgressWindow(const char *);
37 FlashProgressWindow::FlashProgressWindow(const char *wtext
)
38 :eWindow(0), progress(this)
40 move(ePoint(100,250));
41 cresize(eSize(470,50));
43 progress
.move(ePoint(10,10));
44 progress
.resize(eSize(450,30));
48 eFlashtoolMain::eFlashtoolMain()
49 :eListBoxWindow
<eListBoxEntryText
>(_("Flashtool"), 2, 350)
51 move(ePoint(150, 100));
52 new eListBoxEntryText(&list
, _("Save image or part of it"), (void *)0);
53 new eListBoxEntryText(&list
, _("Flash image or part of it"), (void *)1);
54 CONNECT(list
.selected
, eFlashtoolMain::sel_item
);
57 void eFlashtoolMain::sel_item(eListBoxEntryText
*sel
)
61 int flashtool
=(int)sel
->getKey();
69 eFlashtool
flash(flashtool
);
77 eFlashtoolMain::~eFlashtoolMain()
81 eFlashtool::eFlashtool(int direction
)
82 :eListBoxWindow
<eListBoxEntryText
>(_("Partitions"), 12, 400)
84 /* direction == 0 --> read
85 * direction == 1 --> write
88 flashimage
= direction
;
89 move(ePoint(150, 100));
90 //new eListBoxEntryText(&list, _("back"), (void *)-1);
92 FILE* fd
= fopen("/proc/mtd", "r");
95 perror("cannot read /proc/mtd");
101 fgets(buf
,sizeof(buf
), fd
);
104 if (fgets(buf
, sizeof(buf
), fd
) != NULL
)
106 char mtdname
[50] = "";
109 int mtderasesize
= 0;
110 sscanf(buf
, "mtd%d: %x %x \"%[a-zA-Z0-9 =/+`äö<>@^°!?'.:,_-()#\\]\"\n", &mtdnr
, &mtdsize
, &mtderasesize
, mtdname
);
111 new eListBoxEntryText(&list
, _(mtdname
), (void*)mtdnr
);
117 cresize(eSize(400, 10 + entries
* eListBoxEntryText::getEntryHeight()));
118 list
.move(ePoint(10, 5));
119 list
.resize(eSize(getClientSize().width() - 20, getClientSize().height() - 5));
121 CONNECT(list
.selected
, eFlashtool::sel_item
);
124 eFlashtool::~eFlashtool()
128 void eFlashtool::sel_item(eListBoxEntryText
*sel
)
132 mtd
= (int)sel
->getKey();
142 char destination
[100];
143 eFlashtoolSource
imagesource(flashimage
);
145 int res
= imagesource
.exec();
147 strcpy(destination
, imagesource
.getDestination());
151 readmtd(destination
);
154 eMessageBox
box(_("Do you really want to flash a new image now?"), _("Flash"), eMessageBox::btYes
|eMessageBox::btNo
|eMessageBox::iconWarning
, eMessageBox::btNo
);
156 int res
= box
.exec();
158 if (res
== eMessageBox::btYes
)
159 programm(destination
);
165 void eFlashtool::programm(char filename
[])
169 sprintf(mtddev
,"/dev/mtd/%d", mtd
);
171 if (access(filename
, R_OK
) == 0)
174 unsigned long filesize
;
178 if ((fd1
= open(filename
, O_RDONLY
)) < 0)
180 eMessageBox
box(_("Can't read image!"), _("Flash"), eMessageBox::iconInfo
|eMessageBox::btOK
);
187 filesize
= lseek(fd1
, 0, SEEK_END
);
188 lseek(fd1
, 0, SEEK_SET
);
192 eMessageBox
box(_("Image has file size of 0 bytes!"), _("Flash"), eMessageBox::iconInfo
|eMessageBox::btOK
);
199 if ((fd2
= open(mtddev
, O_WRONLY
)) < 0)
201 eMessageBox
box(_("Can't open mtd!"), _("Flash"), eMessageBox::iconInfo
|eMessageBox::btOK
);
210 if (ioctl(fd2
, MEMGETINFO
, &meminfo
) != 0)
215 if (filesize
> meminfo
.size
)
217 eMessageBox
box(_("Image size is too big! Can't flash!"), _("Flash"), eMessageBox::iconInfo
|eMessageBox::btOK
);
224 if (filesize
< ((meminfo
.size
/100)*70))
226 eMessageBox
box(_("Image size is too small! Do you really want to flash it?"), _("Flash"), eMessageBox::btYes
|eMessageBox::btNo
|eMessageBox::iconWarning
, eMessageBox::btNo
);
230 if (res
== eMessageBox::btNo
)
236 eMessageBox
mb(_("Please wait... do NOT switch off the receiver!"),_("flashing in progress"), eMessageBox::iconWarning
);
242 // without this nice we have not enough priority for
243 // file operations... then the update ist very slow on the
247 printf("Starting erasing %s...\n",mtddev
);
251 eMessageBox
box(_("Erase error!"), _("Flash"), eMessageBox::iconInfo
|eMessageBox::btOK
);
258 FlashProgressWindow
wnd(_("Writing image to flash...\n"));
261 char buf
[meminfo
.erasesize
];
262 long fsize
= filesize
;
264 printf("flashing now...\n");
268 if (block
>(long)sizeof(buf
))
272 read(fd1
, buf
, block
);
273 write(fd2
, buf
, block
);
275 wnd
.progress
.setPerc(((filesize
- fsize
) * 100) / filesize
);
282 printf("finished\n");
283 eMessageBox
mbend(_("Flashing successful! restarting..."),
284 _("flashing ok"),eMessageBox::btOK
|eMessageBox::iconInfo
);
288 ::reboot(RB_AUTOBOOT
);
294 eMessageBox
box(_("Image not found!"), _("Flash"), eMessageBox::iconWarning
|eMessageBox::btOK
);
301 bool eFlashtool::erase()
308 mtddev
.sprintf("/dev/mtd/%d", mtd
);
310 if ((fd
= open(mtddev
.c_str(), O_RDWR
)) < 0)
315 if (ioctl(fd
, MEMGETINFO
, &meminfo
) != 0)
320 FlashProgressWindow
wnd(_("Erasing flash..."));
322 erase
.length
= meminfo
.erasesize
;
323 for (erase
.start
= 0; erase
.start
< meminfo
.size
; erase
.start
+= meminfo
.erasesize
)
326 printf("\rErasing %u KByte @ %x -- %2u %% complete.",
327 meminfo
.erasesize
/ 1024, erase
.start
,
328 erase
.start
* 100 / meminfo
.size
);
330 wnd
.progress
.setPerc(erase
.start
* 100 / meminfo
.size
);
331 if (ioctl(fd
, MEMERASE
, &erase
) != 0)
343 bool eFlashtool::readmtd(char destination
[])
350 FILE *fd
= fopen("/.version", "r");
353 sprintf(filename
, "%s/mtd%d.img", destination
, mtd
);
357 fgets(buffer
, sizeof(buffer
), fd
);
361 sscanf(buffer
,"version=%*2s%*2s%8s%4s", date
, time
);
362 sprintf(filename
,"%s/%s_%s_mtd%d.img", destination
, date
, time
, mtd
);
365 sprintf(mtddev
, "/dev/mtd/%d", mtd
);
367 if ((fd1
= open(mtddev
, O_RDONLY
)) < 0)
369 eMessageBox
box(_("Can't open mtd device!"), _("Flash"), eMessageBox::iconWarning
|eMessageBox::btOK
);
376 if (ioctl(fd1
, MEMGETINFO
, &meminfo
) != 0)
381 if ((fd2
= open(filename
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
)) < 0)
383 eString message
= eString(_("Can't open ")) + eString(destination
) + eString(_(" for writing!"));
384 eMessageBox
box(message
.c_str(), _("Flash"), eMessageBox::iconWarning
|eMessageBox::btOK
);
392 filesize
= meminfo
.size
;
394 eMessageBox
mb(_("Please wait while partition is being saved..."),_("saving in progress"), eMessageBox::iconInfo
);
397 FlashProgressWindow
wnd(_("Read Flash..."));
399 char buf
[meminfo
.erasesize
];
400 long fsize
= filesize
;
404 if (block
> (long)sizeof(buf
))
406 read(fd1
, &buf
, block
);
407 write(fd2
, &buf
, block
);
409 wnd
.progress
.setPerc(((filesize
- fsize
) * 100) / filesize
);
415 eString message
= eString(_("Image saved to: ")) + eString(filename
);
416 eMessageBox
boxend(message
.c_str(), _("Flash"), eMessageBox::iconInfo
|eMessageBox::btOK
);
426 eFlashtoolImageView::eFlashtoolImageView(char folder
[])
427 :eListBoxWindow
<eListBoxEntryText
>(_("Image list"), 11, 530)
429 new eListBoxEntryText(&list
, _("back"), (void *)new eString("back"), 0, "Choose your image and press OK...");
431 move(ePoint(100, 100));
433 struct dirent
**namelist
;
434 int n
= scandir(folder
, &namelist
, 0, alphasort
);
437 perror("no flash images available");
441 for(int count
= 0; count
< n
; count
++)
443 eString filen
= namelist
[count
]->d_name
;
444 int pos
= filen
.find(".img");
450 sprintf(tmp
, "%s/%s", folder
, filen
.c_str());
451 if ((fd1
= open(tmp
, O_RDONLY
)) < 0)
452 sprintf(helptext
, "WARNING: can't open this image!");
455 int filesize
= lseek(fd1
, 0, SEEK_END
);
456 lseek(fd1
, 0, SEEK_SET
);
457 sprintf(helptext
, "Image size: %d byte\n", filesize
);
461 new eListBoxEntryText(&list
, filen
, (void *)new eString(tmp
), 0, helptext
);
464 free(namelist
[count
]);
469 cresize(eSize(530, 10 + (entries
> 10 ? 10 : entries
) * eListBoxEntryText::getEntryHeight()));
470 list
.move(ePoint(10, 5));
471 list
.resize(eSize(getClientSize().width() - 20, getClientSize().height() - 30));
473 CONNECT(list
.selected
, eFlashtoolImageView::sel_item
);
475 statusbar
= new eStatusBar(this);
476 statusbar
->move(ePoint(0, clientrect
.height() - 30));
477 statusbar
->resize(eSize(clientrect
.width(), 30));
478 statusbar
->setText("");
479 statusbar
->loadDeco();
482 void eFlashtoolImageView::sel_item(eListBoxEntryText
*sel
)
486 sprintf(buffer
,((eString
*)sel
->getKey())->c_str());
493 char* eFlashtoolImageView::getFilename()
498 eFlashtoolImageView::~eFlashtoolImageView()
502 eFlashtoolSource::eFlashtoolSource(int direction
)
504 :eListBoxWindow
<eListBoxEntryText
>(_("Save to..."), 5, 400)
506 :eListBoxWindow
<eListBoxEntryText
>(_("Save to..."), 3, 400)
513 setText(_("Select source..."));
517 move(ePoint(150, 100));
518 //new eListBoxEntryText(&list, _("back"), (void *)"back");
519 new eListBoxEntryText(&list
, "tmp", (void *)"/tmp");
521 if (access("/hdd/images", W_OK
) == 0)
522 new eListBoxEntryText(&list
, "HDD", (void *)"/hdd/images");
523 if (access("/var/mnt/usb/images", W_OK
) == 0)
524 new eListBoxEntryText(&list
, "USB", (void *)"/var/mnt/usb/images");
526 if (access("/var/mnt/nfs/images", W_OK
) == 0)
527 new eListBoxEntryText(&list
, "NFS", (void *)"/var/mnt/nfs/images");
528 if (access("/var/mnt/cifs/images", W_OK
) == 0)
529 new eListBoxEntryText(&list
, "CIFS", (void *)"/var/mnt/cifs/images");
530 CONNECT(list
.selected
, eFlashtoolSource::sel_item
);
533 void eFlashtoolSource::sel_item(eListBoxEntryText
*sel
)
537 strcpy(buffer
, (char *)sel
->getKey());
539 if (strcmp(buffer
, "back") == 0)
550 eFlashtoolImageView
image(buffer
);
554 strcpy(buffer
, image
.getFilename());
556 if (strcmp(buffer
, "back") == 0 || res
== 0)
563 char* eFlashtoolSource::getDestination()
568 eFlashtoolSource::~eFlashtoolSource()
572 #endif // ENABLE_FLASHTOOL