cvs20080628 - trunk
[gitenigma.git] / src / enigma_dyn_boot.cpp
blob38d9e132a299ff829757ed00b090dcef93ac96d3
1 /*
2 * $Id: enigma_dyn_boot.cpp,v 1.21 2005/12/28 10:42:06 digi_casi Exp $
4 * (C) 2005 by digi_casi <digi_casi@tuxbox.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifdef ENABLE_EXPERT_WEBIF
23 #include <map>
24 #include <time.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <dirent.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <sys/ioctl.h>
33 #include <sys/vfs.h>
34 #include <net/if.h>
35 #include <sys/mount.h>
36 #include <arpa/inet.h>
37 #include <linux/if_ether.h>
38 #include <iomanip>
39 #include <iostream>
40 #include <fstream>
41 #include <lib/system/info.h>
42 #include <lib/system/http_dyn.h>
43 #include <enigma_dyn_utils.h>
44 #include <enigma_dyn_boot.h>
45 #include <bootmenue/bmconfig.h>
46 #define INSTIMAGESUPPORT
47 #include <bootmenue/bmimage.h>
48 #include <bootmenue/bmboot.h>
50 extern eString firmwareLevel(eString versionString);
52 using namespace std;
54 eString getSkins(eString skinPath, eString skinName, eString mountPoint)
56 bmboot bmgr;
57 eString skins;
59 bmgr.getSkins(skinPath, mountPoint);
60 for (unsigned int i = 0; i < bmgr.skinList.size(); i++)
62 eString skin = bmgr.skinList[i];
63 unsigned int pos = skin.find_last_of('/');
64 eString name = skin.right(skin.length() - pos - 1);
65 eString skinPath2 = skin.left(pos);
67 skins = skins + "<option value=\"" + skin + "\"" + eString((skinName == name && skinPath == skinPath2) ? " selected" : "") + ">" + name + "</option>";
70 if (!skins)
71 skins = "<option value=\"" + skinPath + "\">none</option>";
73 return skins;
76 eString getMenus()
78 bool bm = false;
79 bool fw = false;
80 bool fwInstalled = false;
81 eString line;
82 eString result;
83 bmboot bmgr;
85 bmgr.mountJFFS2();
87 ifstream initFile ("/tmp/jffs2/etc/init");
88 if (initFile)
90 while (getline(initFile, line, '\n'))
92 if (line.find("fwpro") != eString::npos)
94 int pos = line.find_first_not_of(" ");
95 fw = line[pos] != '#' && line[pos] != ':';
96 fwInstalled = true;
98 if (line.find("bm.sh") != eString::npos)
100 int pos = line.find_first_not_of(" ");
101 bm = line[pos] != '#' && line[pos] != ':';
105 initFile.close();
107 if (bm && fw)
109 bmgr.activateMenu("BM");
110 fw = false;
113 eString menus = "<option value=\"none\">none</option>";
114 menus += "<option value=\"BM\"" + eString((bm) ? " selected" : "") + ">BootManager</option>";
115 if (fwInstalled)
116 menus += "<option value=\"FW\"" + eString((fw) ? " selected" : "") + ">FlashWizard</option>";
117 result = readFile(TEMPLATE_DIR + "bootMenus.tmp");
118 result.strReplace("#OPTIONS#", menus);
119 if (bm)
120 result.strReplace("#BMSETTINGSBUTTON#", button(100, "Settings", TOPNAVICOLOR, "javascript:editBootManagerSettings('')", "#000000"));
121 else
122 result.strReplace("#BMSETTINGSBUTTON#", "&nbsp;");
124 bmgr.unmountJFFS2();
126 return result;
129 eString getInstalledImages()
131 eString images;
132 bmconfig cfg;
133 bmimages imgs;
134 bmboot bmgr;
136 bmgr.mountJFFS2();
137 cfg.load();
138 bmgr.unmountJFFS2();
140 imgs.load(cfg.mpoint, true);
142 for (unsigned int i = 0; i < imgs.imageList.size(); i++)
144 eString name = imgs.imageList[i].name;
145 eString location = imgs.imageList[i].location;
146 eString image = readFile(TEMPLATE_DIR + "image.tmp");
147 image.strReplace("#NAME#", name);
148 eString version = firmwareLevel(getAttribute(location + "/.version", "version"));
149 image.strReplace("#VERSION#", version);
150 if (location)
152 image.strReplace("#LOCATION#", location);
153 image.strReplace("#SETTINGSBUTTON#", button(80, "Settings", GREEN, "javascript:editImageSettings('" + location + "')", "#FFFFFF"));
154 image.strReplace("#DELETEBUTTON#", button(80, "Delete", RED, "javascript:deleteImage('" + location + "')", "#FFFFFF"));
156 else
158 image.strReplace("#LOCATION#", "&nbsp;");
159 image.strReplace("#SETTINGSBUTTON#", "&nbsp;");
160 image.strReplace("#DELETEBUTTON#", "&nbsp;");
162 images += image;
165 if (!images)
166 images = "<tr><td colspan=\"5\">No images found on selected boot device.</td></tr>";
168 return images;
171 eString getConfigBoot(void)
173 eString result = readFile(TEMPLATE_DIR + "bootMgr.tmp");
174 result.strReplace("#MENU#", getMenus());
175 result.strReplace("#IMAGES#", getInstalledImages());
176 result.strReplace("#ADDIMAGEBUTTON#", button(100, "Add", GREEN, "javascript:showAddImageWindow('')", "#FFFFFF"));
178 return result;
181 eString installImage(eString request, eString dirpath, eString opts, eHTTPConnection *content)
183 bmimages imgs;
184 bmconfig cfg;
185 bmboot bmgr;
186 eString result;
188 bmgr.mountJFFS2();
189 cfg.load();
190 bmgr.unmountJFFS2();
192 std::map<eString, eString> opt = getRequestOptions(opts, '&');
193 eString sourceImage = opt["image"];
194 if (sourceImage != "none")
196 eString mountDir = opt["target"];
197 if (!mountDir)
198 mountDir = cfg.mpoint;
199 eString imageName = opt["name"];
200 if (!imageName)
202 unsigned int pos = sourceImage.find_last_of("/");
203 imageName = sourceImage.right(sourceImage.length() - pos - 1);
204 imageName = imageName.left(imageName.length() - 4);
206 content->local_header["Content-Type"]="text/html; charset=utf-8";
208 int rc = imgs.add(sourceImage, imageName, mountDir);
209 if (rc == 0)
210 result = "<html><head><title>Image installation in process</title></head><body>Enigma will shut down during the installation process and restart once installation is complete.<br><br>Please wait...</body></html>";
211 else
212 result = "<html><head><title>Error during image installation</title></head><body>The image could not be installed due to error: " + eString().sprintf("%d", rc) + "</body></html>";
214 else
215 result = closeWindow(content, "", 10);
217 return result;
220 eString deleteImage(eString request, eString dirpath, eString opts, eHTTPConnection *content)
222 bmimages imgs;
223 std::map<eString, eString> opt = getRequestOptions(opts, '&');
224 eString image = opt["image"];
226 content->local_header["Content-Type"]="text/html; charset=utf-8";
228 imgs.discard(image);
230 return closeWindow(content, "", 10);
233 eString editImageSettings(eString request, eString dirpath, eString opts, eHTTPConnection *content)
235 bmimages imgs;
236 std::map<eString, eString> opt = getRequestOptions(opts, '&');
237 eString image = opt["image"];
239 eString result = readFile(TEMPLATE_DIR + "editImageSettingsWindow.tmp");
241 content->local_header["Content-Type"]="text/html; charset=utf-8";
243 bool reload_modules = (access(eString(image + "/lib/modules/2.6.9/.reload_modules").c_str(), R_OK) == 0);
244 bool fast_boot = (access(eString(image + "/var/etc/.dont_mount_hdd").c_str(), R_OK) == 0);
245 bool dont_start_dccamd = (access(eString(image + "/var/etc/.dont_start_dccamd").c_str(), R_OK) == 0);
247 result.strReplace("#IMAGE#", image);
248 result.strReplace("#NAME#", imgs.getImageName(image));
249 result.strReplace("#RELOADMODULES#", (reload_modules) ? "checked" : "");
250 result.strReplace("#FASTBOOT#", (fast_boot) ? "checked" : "");
251 result.strReplace("#DONTSTARTDCCAMD#", (dont_start_dccamd) ? "checked" : "");
253 return result;
257 eString setImageSettings(eString request, eString dirpath, eString opts, eHTTPConnection *content)
259 bmimages imgs;
261 std::map<eString, eString> opt = getRequestOptions(opts, '&');
263 content->local_header["Content-Type"]="text/html; charset=utf-8";
265 eString image = opt["image"];
266 eString name = opt["name"];
267 eString reload_modules = opt["reload_modules"];
268 eString fast_boot = opt["fast_boot"];
269 eString dont_start_dccamd = opt["dont_start_dccamd"];
271 imgs.setImageName(image, name);
272 if (reload_modules == "on")
273 system(eString("touch " + image + "/lib/modules/2.6.9/.reload_modules").c_str());
274 else
275 system(eString("rm " + image + "/lib/modules/2.6.9/.reload_modules").c_str());
277 if (fast_boot == "on")
278 system(eString("touch " + image + "/var/etc/.dont_mount_hdd").c_str());
279 else
280 system(eString("rm " + image + "/var/etc/.dont_mount_hdd").c_str());
282 if (dont_start_dccamd == "on")
283 system(eString("touch " + image + "/var/etc/.dont_start_dccamd").c_str());
284 else
285 system(eString("rm " + image + "/var/etc/.dont_start_dccamd").c_str());
287 return WINDOWCLOSE;
290 eString editBootManagerSettings(eString request, eString dirpath, eString opts, eHTTPConnection *content)
292 std::map<eString, eString> opt = getRequestOptions(opts, '&');
294 content->local_header["Content-Type"]="text/html; charset=utf-8";
296 bmconfig cfg;
297 bmboot bmgr;
299 bmgr.mountJFFS2();
300 cfg.load();
301 bmgr.unmountJFFS2();
303 eString result = readFile(TEMPLATE_DIR + "bootMgrSettings.tmp");
304 result.strReplace("#SKINOPTIONS#", getSkins(SKINDIR, cfg.skinName, cfg.mpoint));
305 result.strReplace("#MPOINT#", cfg.mpoint);
306 result.strReplace("#SELECTEDENTRY#", cfg.selectedEntry);
307 result.strReplace("#RANDOMSKIN#", cfg.randomSkin);
308 result.strReplace("#TIMEOUTVALUE#", cfg.timeoutValue);
309 result.strReplace("#VIDEOFORMAT#", cfg.videoFormat);
310 result.strReplace("#SKINPATH1#", cfg.skinPath);
311 result.strReplace("#SKINPATH2#", cfg.mpoint + "/boot/skins");
312 result.strReplace("#SKINNAME#", cfg.skinName);
314 result.strReplace("#BUTTONSUBMIT#", button(100, "Change", TOPNAVICOLOR, "javascript:submitSettings()", "#000000"));
316 return result;
319 eString setBootManagerSettings(eString request, eString dirpath, eString opts, eHTTPConnection *content)
321 std::map<eString, eString> opt = getRequestOptions(opts, '&');
322 bmconfig cfg;
323 bmboot bmgr;
325 content->local_header["Content-Type"]="text/html; charset=utf-8";
327 cfg.mpoint = opt["mpoint"];
328 cfg.selectedEntry = opt["selectedEntry"];
329 cfg.randomSkin = opt["randomSkin"];
330 cfg.timeoutValue = opt["timeoutValue"];
331 cfg.videoFormat = opt["videoFormat"];
332 cfg.skinPath = opt["skinPath1"];
333 cfg.skinName = opt["skinName"];
334 unsigned int pos = cfg.skinName.find_last_of("/");
335 if (pos != eString::npos && pos > 0)
336 cfg.skinName = cfg.skinName.right(cfg.skinName.length() - pos - 1);
338 bmgr.mountJFFS2();
339 cfg.save();
340 bmgr.unmountJFFS2();
342 return WINDOWCLOSE;
345 eString selectBootMenu(eString request, eString dirpath, eString opts, eHTTPConnection *content)
347 bmboot bmgr;
348 std::map<eString, eString> opt = getRequestOptions(opts, '&');
349 eString menu = opt["menu"];
351 content->local_header["Content-Type"]="text/html; charset=utf-8";
353 bmgr.activateMenu(menu);
355 return closeWindow(content, "", 10);
358 eString showAddImageWindow(eString request, eString dirpath, eString opts, eHTTPConnection *content)
360 bmconfig cfg;
361 bmboot bmgr;
363 bmgr.mountJFFS2();
364 cfg.load();
365 bmgr.unmountJFFS2();
367 std::map<eString, eString> opt = getRequestOptions(opts, '&');
369 content->local_header["Content-Type"]="text/html; charset=utf-8";
371 eString result = readFile(TEMPLATE_DIR + "addImageWindow.tmp");
373 eString images;
374 if (DIR *p = opendir(cfg.mpoint.c_str()))
376 while (struct dirent *e = readdir(p))
378 eString name = cfg.mpoint + "/" + e->d_name;
379 eString tmp = name;
380 tmp.upper();
381 if (tmp.find(".IMG") != eString::npos)
382 images += "<option value=\"" + name + "\">" + e->d_name + "</option>\n";
384 closedir(p);
386 if (!images)
387 images = "<option value=\"none\">none</option>";
389 result.strReplace("#MPOINT#", cfg.mpoint);
390 result.strReplace("#IMAGES#", images);
392 return result;
395 void ezapBootManagerInitializeDyn(eHTTPDynPathResolver *dyn_resolver, bool lockWeb)
397 dyn_resolver->addDyn("GET", "/cgi-bin/installimage", installImage, lockWeb);
398 dyn_resolver->addDyn("GET", "/cgi-bin/deleteimage", deleteImage, lockWeb);
399 dyn_resolver->addDyn("GET", "/cgi-bin/editimagesettings", editImageSettings, lockWeb);
400 dyn_resolver->addDyn("GET", "/cgi-bin/setimagesettings", setImageSettings, lockWeb);
401 dyn_resolver->addDyn("GET", "/cgi-bin/selectbootmenu", selectBootMenu, lockWeb);
402 dyn_resolver->addDyn("GET", "/cgi-bin/showaddimagewindow", showAddImageWindow, lockWeb);
403 dyn_resolver->addDyn("GET", "/cgi-bin/editbootmanagersettings", editBootManagerSettings, lockWeb);
404 dyn_resolver->addDyn("GET", "/cgi-bin/setbootmanagersettings", setBootManagerSettings, lockWeb);
406 #endif