2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 local io = require "io"
17 local os = require "os"
18 local fs = require "luci.fs"
19 local util = require "luci.util"
20 local ltn12 = require "luci.ltn12"
21 local posix = require "posix"
23 local type, assert, error = type, assert, error
25 module "luci.sys.mtdow"
42 EmulatedWriter = util.class(Writer)
43 EmulatedWriter.blocks = {
47 write = WRITE_SEPARATELY
51 function EmulatedWriter.write_block(self, name, imagestream, appendpattern)
53 os.execute("grep rootfs /proc/mtd >/dev/null || "
54 .. "{ echo /dev/hda2,65536,rootfs > "
55 .. "/sys/module/block2mtd/parameters/block2mtd }")
57 return Writer.write_block(self, name, imagestream, appendpattern)
61 CFEWriter = util.class(Writer)
64 magic = {"4844", "5735"},
71 CommonWriter = util.class(Writer)
72 CommonWriter.blocks = {
75 write = WRITE_COMBINED
80 RedWriter = util.class(Writer)
83 device = "vmlinux.bin.l7",
88 write = WRITE_COMBINED
93 function native_writer()
97 local x86 = {"x86_64", "i386", "i486", "i586", "i686"}
98 if util.contains(x86, posix.uname("%m")) then
99 return EmulatedWriter()
103 if w:_find_mtdblock("cfe") and w:_find_mtdblock("linux") then
108 if w:_find_mtdblock("RedBoot") and w:_find_mtdblock("vmlinux.bin.l7") then
113 if fs.readfile("/proc/cpuinfo"):find("MagicBox") then
114 return CommonWriter()
120 Writer.MTD = "/sbin/mtd"
121 Writer.SAFEMTD = "/tmp/mtd"
122 Writer.IMAGEFIFO = "/tmp/mtdimage.fifo"
124 function Writer.write_block(self, name, imagestream, appendfile)
125 assert(self.blocks[name], ERROR_NOTFOUND)
126 local block = self.blocks[name]
127 local device = block.device
128 device = fs.stat(device) and device or self:_find_mtdblock(device)
129 assert(device, ERROR_NODETECT)
131 imagestream = self:_check_magic(imagestream, block.magic)
133 assert(imagestream, ERROR_INVMAGIC)
136 if block.write == WRITE_COMBINED then
137 return (self:_write_combined(device, imagestream, appendfile) == 0)
138 elseif block.write == WRITE_EMULATED then
139 return (self:_write_emulated(device, imagestream, appendfile) == 0)
141 error(ERROR_NOTAVAIL)
144 return (self:_write_memory(device, imagestream) == 0)
148 function Writer._check_magic(self, imagestream, magic)
149 magic = type(magic) == "table" and magic or {magic}
151 local block = imagestream()
152 assert(block, ERROR_NOSTREAM)
153 local cm = "%x%x" % {block:byte(1), block:byte(2)}
155 if util.contains(magic, cm) then
156 return ltn12.source.cat(ltn12.source.string(block), imagestream)
160 function Writer._find_mtdblock(self, mtdname)
162 local prefix = "/dev/mtd"
163 prefix = prefix .. (fs.stat(prefix) and "/" or "")
165 for l in io.lines("/proc/mtd") do
166 local k = l:match('mtd([%%w-_]+):.*"%s"' % mtdname)
167 if k then return prefix..k end
171 function Writer._write_emulated(self, devicename, imagestream, appendfile)
172 local stat = (self:_write_memory(device, imagestream) == 0)
173 stat = stat and (self:_refresh_block("rootfs") == 0)
174 local squash = self:_find_mtdblock("rootfs_data")
176 stat = stat and (self:_append("rootfs_data", imagestream, true) == 0)
178 stat = stat and (self:_append("rootfs", imagestream) == 0)
183 function Writer._write_memory(self, devicename, imagestream)
184 local imageproc = posix.fork()
185 assert(imageproc ~= -1, ERROR_RESOURCE)
186 if imageproc == 0 then
187 fs.unlink(self.IMAGEFIFO)
188 assert(posix.mkfifo(self.IMAGEFIFO), ERROR_RESOURCE)
189 local imagefifo = io.open(self.IMAGEFIFO, "w")
190 assert(imagefifo, ERROR_RESOURCE)
191 ltn12.pump.all(imagestream, ltn12.sink.file(imagefifo))
196 "%s write '%s' '%s'" % {
197 self.MTD, self.IMAGEFIFO, devicename
202 function Writer._write_combined(self, devicename, imagestream, appendfile)
203 local imageproc = posix.fork()
204 assert(imageproc ~= -1, ERROR_RESOURCE)
205 if imageproc == 0 then
206 fs.unlink(self.IMAGEFIFO)
207 assert(posix.mkfifo(self.IMAGEFIFO), ERROR_RESOURCE)
208 local imagefifo = io.open(self.IMAGEFIFO, "w")
209 assert(imagefifo, ERROR_RESOURCE)
210 ltn12.pump.all(imagestream, ltn12.sink.file(imagefifo))
215 "%s -j '%s' write '%s' '%s'" % {
216 self.MTD, appendfile, self.IMAGEFIFO, devicename
221 function Writer._refresh_block(self, devicename)
222 return os.execute("%s refresh '%s'" % {self.MTD, devicename})
225 function Writer._append(self, devicename, appendfile, erase)
226 erase = erase and ("-e '%s' " % devicename) or ''
229 "%s %s jffs2write '%s' '%s'" % {
230 self.MTD, erase, appendfile, devicename