rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
| 5 | <script> |
| 6 | $(document).ready(function() { |
| 7 | let xmldoc = null; |
| 8 | |
| 9 | function check() { |
| 10 | $("#out").text(""); |
| 11 | let hdr = xmldoc.getElementsByTagName("partition")[0]; |
| 12 | if (hdr == null) { |
| 13 | $("#out").text("Error: invlaid partition file\n"); |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | let entries = xmldoc.getElementsByTagName("entry"); |
| 18 | for (let i=0; i<entries.length; i++) { |
| 19 | if (parseInt(entries[i].getAttribute("end")) >= parseInt(hdr.getAttribute("lba"))) { |
| 20 | let error_msg = "Error: end lba of " + entries[i].getAttribute("name") + " (" + entries[i].getAttribute("end") + ") >= total lba (" + hdr.getAttribute("lba") + ")\n\n"; |
| 21 | $("#out").text(error_msg); |
| 22 | return; |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | function show() { |
| 28 | if (xmldoc == null) |
| 29 | return; |
| 30 | |
| 31 | $("#total_size").text((eval($("#total_lba").val()) * eval($("#lbs").val()) / 1024 / 1024) + "MB"); |
| 32 | $("#start_addr").text((eval($("#start_lba").val()) * eval($("#lbs").val()) / 1024 / 1024) + "MB"); |
| 33 | |
| 34 | let text = ""; |
| 35 | text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmldoc.documentElement.outerHTML; |
| 36 | text = text.replace(/>\s*</gm, ">\n<"); |
| 37 | text = text.replace(/^<entry/gm, "\t<entry"); |
| 38 | text = text.replace(/^\s+\n/gm, ""); |
| 39 | $("#out").text($("#out").text() + text); |
| 40 | } |
| 41 | |
| 42 | function init_editor() { |
| 43 | if (xmldoc == null) |
| 44 | return; |
| 45 | |
| 46 | let hdr = xmldoc.getElementsByTagName("partition")[0]; |
| 47 | $("#total_lba").val(hdr.getAttribute("lba")); |
| 48 | if (hdr.getAttribute("lbs") != null) |
| 49 | $("#lbs").val(hdr.getAttribute("lbs")); |
| 50 | else |
| 51 | $("#lbs").val(512); |
| 52 | |
| 53 | let entries = xmldoc.getElementsByTagName("entry"); |
| 54 | $("#start_lba").val(entries[0].getAttribute("start")); |
| 55 | |
| 56 | let text = ""; |
| 57 | for (let i=0; i<entries.length; i++) { |
| 58 | let size = ((entries[i].getAttribute("end") - entries[i].getAttribute("start") + 1)*$("#lbs").val()/1024); |
| 59 | text += (entries[i].getAttribute("name") + " " + size + "\n"); |
| 60 | } |
| 61 | $("#entry_editor").val(text); |
| 62 | } |
| 63 | |
| 64 | function update_xml() { |
| 65 | if (xmldoc == null) |
| 66 | return; |
| 67 | |
| 68 | let hdr = xmldoc.getElementsByTagName("partition")[0]; |
| 69 | hdr.setAttribute("lba", eval($("#total_lba").val())); |
| 70 | |
| 71 | if (eval($("#lbs").val()) == 512) |
| 72 | hdr.removeAttribute("lbs"); |
| 73 | else |
| 74 | hdr.setAttribute("lbs", eval($("#lbs").val())); |
| 75 | |
| 76 | let entries = xmldoc.getElementsByTagName("entry"); |
| 77 | let entry_strs = $("#entry_editor").val().split("\n"); |
| 78 | |
| 79 | let entry_cnt = 0; |
| 80 | for (let i=0; i<entry_strs.length; i++) { |
| 81 | if (entry_strs[i] == "") |
| 82 | continue; |
| 83 | |
| 84 | if (entry_cnt >= entries.length) { |
| 85 | let newEle = xmldoc.createElement("entry"); |
| 86 | newEle.setAttribute("type", entries[entry_cnt-1].getAttribute("type")); |
| 87 | hdr.appendChild(newEle); |
| 88 | entries = xmldoc.getElementsByTagName("entry"); |
| 89 | } |
| 90 | |
| 91 | entry = entry_strs[i].trim().split(/\s+/); |
| 92 | if (entry.length < 2) |
| 93 | continue; |
| 94 | if (entry_cnt == 0) |
| 95 | entries[entry_cnt].setAttribute("start", eval($("#start_lba").val())); |
| 96 | else |
| 97 | entries[entry_cnt].setAttribute("start", parseInt(entries[entry_cnt-1].getAttribute("end")) + 1); |
| 98 | entries[entry_cnt].setAttribute("end", (parseInt(entries[entry_cnt].getAttribute("start")) + eval(entry[1])*1024/eval($("#lbs").val()) - 1)); |
| 99 | entries[entry_cnt].setAttribute("name", entry[0]); |
| 100 | entry_cnt++; |
| 101 | } |
| 102 | |
| 103 | for (; entry_cnt < entries.length;) { |
| 104 | hdr.removeChild(entries[entry_cnt]); |
| 105 | entries = xmldoc.getElementsByTagName("entry"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $("#partition").change(function() { |
| 110 | let reader = new FileReader(); |
| 111 | reader.onload = function() { |
| 112 | let parser = new DOMParser(); |
| 113 | xmldoc = parser.parseFromString(reader.result, "application/xml"); |
| 114 | init_editor(); |
| 115 | check(); |
| 116 | show(); |
| 117 | }; |
| 118 | reader.readAsText($("#partition")[0].files[0]); |
| 119 | }); |
| 120 | |
| 121 | $(":text,#entry_editor").keyup(function(){ |
| 122 | update_xml(); |
| 123 | check(); |
| 124 | show(); |
| 125 | }); |
| 126 | |
| 127 | $(":text").change(function(){ |
| 128 | $(":focus").val(eval($(":focus").val())); |
| 129 | }); |
| 130 | |
| 131 | $("#entry_editor").change(function(){ |
| 132 | let text = ""; |
| 133 | let entry_strs = $("#entry_editor").val().split("\n"); |
| 134 | |
| 135 | for (let i=0; i<entry_strs.length; i++) { |
| 136 | if (entry_strs[i] == "") |
| 137 | continue; |
| 138 | |
| 139 | let entry = entry_strs[i].trim().split(/\s+/); |
| 140 | if (entry.length < 2) |
| 141 | continue; |
| 142 | |
| 143 | text += entry[0] + " " + eval(entry[1]) + "\n"; |
| 144 | } |
| 145 | |
| 146 | $("#entry_editor").val(text); |
| 147 | }); |
| 148 | |
| 149 | }); |
| 150 | </script> |
| 151 | </head> |
| 152 | <body> |
| 153 | <label>partition file: |
| 154 | <input type=file id="partition"> |
| 155 | </label> |
| 156 | <br> |
| 157 | <hr> |
| 158 | |
| 159 | <label>lba size(lbs): |
| 160 | <input type=text id="lbs"> |
| 161 | </label> |
| 162 | <br> |
| 163 | <label>total lba: |
| 164 | <input type=text id="total_lba"> |
| 165 | </label> |
| 166 | <label id="total_size"> |
| 167 | </label> |
| 168 | <br> |
| 169 | <hr> |
| 170 | |
| 171 | <label>start lba of the first partition: |
| 172 | <input type=text id="start_lba"> |
| 173 | </label> |
| 174 | <label id="start_addr"> |
| 175 | </label> |
| 176 | <br> |
| 177 | <label> name size(KB) |
| 178 | </label> |
| 179 | <br> |
| 180 | <textarea id=entry_editor rows=20> |
| 181 | </textarea> |
| 182 | <br> |
| 183 | <hr> |
| 184 | |
| 185 | <pre id="out"></pre> |
| 186 | </body> |
| 187 | </html> |