Revised sysupgrade part 1
[project/luci.git] / libs / sys / luasrc / sys / mtdow.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
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"
22
23 local type, assert, error = type, assert, error
24
25 module "luci.sys.mtdow"
26
27 WRITE_IMAGE = 0
28 WRITE_COMBINED = 1
29 WRITE_EMULATED = 2
30
31 ERROR_INTERNAL = 1
32 ERROR_NOTFOUND = 2
33 ERROR_RESOURCE = 3
34 ERROR_NODETECT = 4
35 ERROR_NOTAVAIL = 5
36 ERROR_NOSTREAM = 6
37 ERROR_INVMAGIC = 7
38
39 Writer = util.class()
40
41 -- x86
42 EmulatedWriter = util.class(Writer)
43 EmulatedWriter.blocks = {
44         image = {
45                 magic = "eb48",
46                 device = "/dev/hda",
47                 write = WRITE_EMULATED
48         }
49 }
50
51 function EmulatedWriter.write_block(self, name, imagestream, appendpattern)
52         if appendpattern then
53                 os.execute("grep rootfs /proc/mtd >/dev/null || "
54                         .. "{ echo /dev/hda2,65536,rootfs > "
55                         .. "/sys/module/block2mtd/parameters/block2mtd }")
56         end
57         return Writer.write_block(self, name, imagestream, appendpattern)
58 end
59
60 -- Broadcom
61 CFEWriter = util.class(Writer)
62 CFEWriter.blocks = {
63         image = {
64                 magic = {"4844", "5735"},
65                 device = "linux",
66                 write = WRITE_IMAGE
67         }
68 }
69
70 -- Magicbox
71 CommonWriter = util.class(Writer)
72 CommonWriter.blocks = {
73         image = {
74                 device = "linux",
75                 write = WRITE_COMBINED
76         }
77 }
78
79 -- Atheros
80 RedWriter = util.class(Writer)
81 RedWriter.blocks = {
82         kernel = {
83                 device = "vmlinux.bin.l7",
84                 write = WRITE_IMAGE
85         },
86         rootfs = {
87                 device = "rootfs",
88                 write = WRITE_COMBINED
89         } 
90 }
91
92 -- Auto Detect
93 function native_writer()
94         local w = Writer()
95         
96         -- Detect amd64 / x86
97         local x86 = {"x86_64", "i386", "i486", "i586", "i686"}
98         if util.contains(x86, posix.uname("%m")) then
99                 return EmulatedWriter()
100         end
101         
102         -- Detect CFE
103         if w:_find_mtdblock("cfe") and w:_find_mtdblock("linux") then
104                 return CFEWriter()
105         end
106         
107         -- Detect Redboot
108         if w:_find_mtdblock("RedBoot") and w:_find_mtdblock("vmlinux.bin.l7") then
109                 return RedWriter()
110         end
111         
112         -- Detect MagicBox
113         if fs.readfile("/proc/cpuinfo"):find("MagicBox") then
114                 return CommonWriter() 
115         end
116 end
117
118
119
120 Writer.MTD = "/sbin/mtd"
121 Writer.SAFEMTD = "/tmp/mtd"
122 Writer.IMAGEFIFO = "/tmp/mtdimage.fifo"
123
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)
130         if block.magic then
131                 imagestream = self:_check_magic(imagestream, block.magic)
132         end
133         assert(imagestream, ERROR_INVMAGIC)
134         
135         if appendfile then
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)
140                 else
141                         error(ERROR_NOTAVAIL)
142                 end
143         else
144                 return (self:_write_memory(device, imagestream) == 0)
145         end
146 end
147
148 function Writer._check_magic(self, imagestream, magic)
149         magic = type(magic) == "table" and magic or {magic}
150         
151         local block = imagestream()
152         assert(block, ERROR_NOSTREAM)
153         local cm = "%x%x" % {block:byte(1), block:byte(2)}
154         
155         if util.contains(magic, cm) then
156                 return ltn12.source.cat(ltn12.source.string(block), imagestream)
157         end
158 end
159
160 function Writer._find_mtdblock(self, mtdname)
161         local k
162         local prefix = "/dev/mtd"
163         prefix = prefix .. (fs.stat(prefix) and "/" or "")
164         
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
168         end
169 end
170
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")
175         if squash then
176                 stat = stat and (self:_append("rootfs_data", imagestream, true) == 0)
177         else
178                 stat = stat and (self:_append("rootfs", imagestream) == 0)
179         end
180         return stat
181 end
182
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))
192                 os.exit(0)
193         end
194         
195         return os.execute( 
196                 "%s write '%s' '%s' >/dev/null 2>&1" % {
197                 self.MTD, self.IMAGEFIFO, devicename
198                 }
199         )       
200 end
201
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))
211                 os.exit(0)
212         end
213         
214         return os.execute( 
215                 "%s -j '%s' write '%s' '%s' >/dev/null 2>&1" % {
216                         self.MTD, appendfile, self.IMAGEFIFO, devicename
217                 }
218         )
219 end
220
221 function Writer._refresh_block(self, devicename)
222         return os.execute("%s refresh '%s' >/dev/null 2>&1" % {self.MTD, devicename})
223 end
224
225 function Writer._append(self, devicename, appendfile, erase)
226         erase = erase and ("-e '%s' " % devicename) or ''
227         
228         return os.execute( 
229                 "%s %s jffs2write '%s' '%s' >/dev/null 2>&1" % {
230                         self.MTD, erase, appendfile, devicename
231                 }
232         )
233 end