start	end	text
0	1140	Okay, hello.
1140	8620	Welcome to this talk, Emulating Embedded Linux Devices at Scale with Light Touch Firmware Rehosting.
8880	28080	Today I'm going to give this a kind of higher level approach, like overview of what we've been doing in the advanced research team at CrowdStrike, which is kind of giving us this quite low investment, high return ratio when we've been emulating embedded Linux systems.
28080	36620	So by the end, you should be able to kind of take the principles that I'm talking about here and apply them in other emulation stuff.
36620	43580	It's a little bit of a patchwork approach and a bit proof of concept, but hopefully a little bit helpful at least.
44840	73220	At the moment, most embedded Linux emulation works in user space, so a lot of the emulation approaches take a stock ARM or MIPS KME machine, boot into like a normal distro, unpack the firmware into that guest, CH root into it and then run binaries with like a LD preload to paper over incompatibilities in it.
73920	79860	This approach works pretty well if you're looking at a specific binary that you want to look at on a specific device.
80420	85500	There are some cases where it gets pretty tricky, but in a lot of cases, it's totally fine to do.
85760	101400	But when you're doing that kind of approach with the LD preload approach, what you're basically trying to do is compensate for missing peripherals and a different kind of system set up that the binaries are not expecting.
102380	105120	So NV RAM is the classic example.
105840	116300	The user space way to deal with NV RAM is to LD preload something like libNVRAM or some descendant of that with extra functionality.
117580	127820	But there's certain cases where we can't really address these kind of incompatibilities just by LD preloading stuff.
127820	135260	When the systems are designed to work more holistically with a lot more moving parts.
138040	143400	So the traditional kind of way of doing things in user space doesn't really work for those kind of approaches.
144360	148040	So our question was, can we try and do it in a different way?
148860	152200	We want to do vulnerability research on these systems.
152380	154760	We just want to do things like run honey pots.
155280	157540	So we want something like a full system emulation.
157540	162220	We want the whole thing to be running as it usually would on the device.
163260	172660	But we can't really emulate just like a specific device in full without a lot of time and effort and the payoff might be kind of low for how much time we'd have to put in.
173120	179640	So we want to see if there's another approach that we could try and try a few different things.
179640	182060	And we came to this.
182060	184360	This kind of re-hosting concept.
184920	188520	The main idea of this is we don't touch user space at all.
188520	190960	We don't want to do anything in user space.
190960	192460	We don't want to LD preload anything.
192460	195920	We don't want to do any hacks during the boot process.
196140	200920	We just want to offer up a firmware image to the re-hosting environment.
201240	205600	Ideally the exact same firmware image that's bundled in a firmware update image.
205840	207660	And for it just to run.
209740	213280	User space can only kind of interact with what the kernel gives it.
214180	221160	And we, the re-hosting approach means we can make loads of changes in the kernel and do as much as we want in the kernel.
221280	225620	But we just kind of create an environment.
225680	227460	Create this re-hosting environment for it.
228240	233720	So the idea is we would build a kernel for a particular KME machine with a compatible architecture.
234020	241700	And then modify the kernel to create an environment which looks as close to a real device as possible from the perspective of user space.
241960	248240	And then we can just tweak the kernel, tweak the KME machine, like add peripherals, add hardware whenever we want.
248420	250140	Or whenever we need to.
251060	254320	The question is then like where do we start with something like this?
256220	259720	All embedded systems are running the Linux kernel.
259720	265220	But they're running a Linux kernel built for a particular system on chip or sock.
267260	271160	A sock is just a bundle of CPU memory, other peripherals, things like that.
271240	272720	Networking, storage.
272720	274620	It depends on the sock.
275460	278500	Some peripherals might be integrated directly into the sock.
278900	285940	Others might have like a physical interface that the hardware devs can wire another chip to, like flash memory or something like that.
286320	291120	But each sock is designed by a hardware to cater to a particular market.
291120	296240	So a specific sock will get used in lots of different embedded systems, sold by lots of different vendors.
296560	303060	So Netgear, Asus, TP Link, they don't buy, they don't build the sock themselves.
303060	304320	They're not chip companies.
304320	307560	They buy a sock from a manufacturer and then use them in their devices.
308340	313820	And so the same sock is used across multiple devices by multiple different vendors.
314940	317040	So the supply chain looks like this.
317630	323580	The sock manufacturer makes the socks, so like Broadcom, Qualcomm, Media Tech, all these kinds of companies.
323940	332420	Vendors buy the socks from the manufacturer and the manufacturers give the vendors some kind of SDK they can build their own firmware with to run on that sock.
332920	336620	The vendor will use that SDK to build the kernel and file system binaries.
338420	343700	So it kind of means that any code running close to the hardware is usually manufacturer provided.
343880	346500	Like vendors won't really have much to do with that.
346760	349800	And the vendors can do whatever they want in user space.
349800	356360	They just use their like APIs and libraries or whatever provided by the manufacturer as part of the SDK as well.
358620	360480	Well, what does that mean for us?
360480	363000	Well, we're only trying to make changes to the kernel.
363780	376680	So instead of emulating a specific device by like a specific vendor, we think about like we're trying to emulate the kernel or like create a new kernel that looks a lot like one for a specific sock.
377800	382680	We're not trying to emulate the hardware of the sock itself and try and run a prebuilt kernel on it.
382900	384140	We did try that.
384140	385820	It's a bit tricky.
387180	393700	So we want to just build a kernel and sort of tweak a KMU machine if we need to.
393700	401460	So that from the perspective of user space, user space thinks it's running on that particular sock kernel combo.
401940	409140	The benefit of this in theory is if we can do this, then we should be able to take firmware from any device based on that sock and run it.
409140	414520	So we could build one rehosting environment and get a few dozen possible emulated devices out of it.
416720	420040	We've got a lot of things we can kind of lean on to try and do this.
420040	426280	We don't obviously have data sheets from the manufacturers for these socks.
426280	428320	We don't have kind of proprietary information.
428460	433340	We do have KMU machines which are probably good enough for getting started.
433340	434760	We have the Linux kernel.
434800	437880	We have firmware images for devices that we want to emulate.
437880	439860	We have reverse engineering tools.
440080	442880	We have GPL code in a lot of cases.
443320	445300	We have compiled kernels.
445300	449100	We have kernel modules that come with the firmware images.
449120	460240	We have things like open WRT, DDWRT, kind of open source distros which might have releases to run, which run on the sock we're trying to target.
460240	461920	They kind of do the opposite of what we're trying to do.
461920	465740	They're trying to run on the actual hardware and we're trying to not run on the actual hardware.
465740	467220	But they're kind of similar.
467220	470420	We have kind of similar but opposite intentions.
471420	473140	We don't have data sheets.
473140	476540	We don't have really that much source code for binary blobs.
476700	478100	We don't have documentation.
478100	482900	We just don't have unlimited time and energy or unlimited money.
482900	486660	We're just trying to get to a point where the thing runs well enough.
488360	490220	So this is the vague plan.
490220	498060	We start with running a mainline kernel, similar to a version, a similar version to the one that actually runs on a normal device.
498260	502080	And then we compile that to run on the most compatible KMU machine we can find.
503000	506560	And then on that machine we load the file system and just try and run stuff.
506560	509520	And we see what breaks and we try and figure it out.
510060	515320	And then we patch the kernel again and maybe make modifications to the KMU machine.
515320	519600	And we just keep doing this in this kind of twisted development life cycle.
520780	530080	We can, during all this, we can infer behavior from the user space to kind of figure out what it's actually expecting and what it wants to see and what it's doing.
530080	537060	And then we can make changes reflectively in the kernel to cover that.
538060	539940	So this was the device we chose to do this.
539940	541540	It's an older Asus router.
542100	545400	It's based, it's the AC87U.
545800	547640	It was released in 2014.
547660	550000	It went end of life probably 2021.
550240	554480	It's based around the BCM4709, which was your Broadcom sock.
555980	560500	And the 4709 is used in quite a few routers.
560600	563660	It's a router sock with a Cortex A9 core.
563660	569880	It's in the broader BCM4708, 4707, 4708, 4709 family.
570040	572580	It has an integrated ethernet stack.
573260	578620	It was built for the router market so it gets used in routers and extenders and things like that.
578620	580380	The list up here is kind of non-exhaustive.
580380	593640	It's not every router that runs with this, that has this sock, but it's a list, it gives a kind of indication of what different vendors, which different vendors were using it and what kind of device it's being used on.
596160	599560	So the AC87U was kind of chosen at random.
600680	607820	The idea was we were kind of looking at Asus stuff and we wanted to just kind of see how easy it would be to do emulation for them.
607820	615520	But the idea is that whatever we build for the 87U would maybe work for other Asus devices running on the same sock and there's a lot of them.
616460	619580	I should also say we didn't actually have the physical device for this at all.
619580	621700	We worked only with the firmware image.
621800	624720	So there's also a bit of an artificial restriction.
625800	628260	But I kind of just wanted to see if it could be done.
628260	638340	There's a lot of common wisdom when it comes to embedded systems emulation that you need the physical device to do it, to like dump the NV RAM and get all these things out of it.
638340	642200	But our intention longer term is to emulate devices at scale.
642200	645320	So we don't necessarily want to buy every single device that we want to emulate.
645320	647660	So it's a bit of an experiment in this as well.
649480	654240	The first step for this we can just get the firmware.
654240	656400	And we get whatever is publicly available.
656760	660900	We can get the most recent firmware from the Asus website from 2021.
661140	665800	Get the GPL code which has some like Broadcom commented code in there as well.
666540	671460	The firmware image is in this TRX format which is like an older Broadcom firmware file format.
671460	674540	We can unpack these like really easily with Unblob.
674780	678860	And using Unblob also means we can carve out the specific sections of the firmware.
678920	682140	And we don't have to do any post processing stuff like we'd have to do with Binwalk.
682140	685680	So you can carve everything out very precisely with Unblob.
687240	692080	Once it's unpacked, so we get this Lisma compressed kernel and we get the SquashFS file system.
695150	699990	We can, with this unpacked firmware we can kind of get a sense of what the device is actually running.
701070	704170	What it's running on.
705010	706470	And so what we need to emulate.
706470	714410	So we get the kernel version out, a string out of the kernel which is 2.6.36.4 BRCM arm.
715330	719830	It's this Linux 2.6 with some Broadcom modifications I guess.
719970	727050	It's compiled with GCC 4.3, 4.5.3 from build root 2012.2.
728550	730390	So we know what it's compiled with as well.
730390	735970	We can use the same compiler and just kind of keep things as similar as possible when we're building our own kernel.
736530	738550	We can also get the boot args out of the kernel blob.
738550	741030	So this can give us an idea of how it's going to boot.
741270	747790	They might not be the actual boot args that the real device is using but it's a decent way to start.
748110	751790	So the root boot arg points to MTD block 2.
751790	754390	So it's booting from this particular part in flash.
755290	758670	The init boot arg points to S bin pre init.
759770	766570	If we look at the unblob file system we can see that pre init is a link to a binary called RC.
766570	768890	There's no init scripts or anything like this.
768890	772390	It's just this RC binary being run as pre init.
774110	776210	A lot of other binaries linked to RC.
776210	778870	It's a pretty big sort of system functionality binary.
779550	780710	And it checks how it's invoked.
780710	785290	So it will run in this pre init mode when it's run as pre init.
785330	788110	Or in another mode if it's run with a different name.
789350	794030	The layout of the unpack file system tells us a lot how the system is expected to run.
794630	801690	Kind of directions we'd usually expect to see useful stuff in populated or actually just broken sim links to temp.
801710	806790	So temp is probably populated at boot time, probably by the init process, probably by pre init in particular.
808230	817570	And so in order to get, when we're doing our emulation we want to get the system into a state using the normal init process they're expecting to perform.
819050	824310	So that we can run all the other binaries and things on it and the system will be up kind of in a normal state.
824310	829570	So we don't want to go through and kind of paper over these things and do this stuff in user space.
829570	834850	We just want to make sure that the init process is capable of kind of completing.
836070	840630	So this is, we can apply this same kind of life cycle that we had before to this setup.
841010	848750	We know the architecture, kernel version, compiler, the way the file system has been loaded from memory, from flash sorry.
849970	858650	So the first kind of naive plan which is definitely going to fail in the first instance is like build a KMU machine with a cortex A9 core, sorry find one.
858650	863450	Build a 2.6.36.4 kernel for it.
863910	867570	Attach the squash FS image which we carved out earlier.
870070	878590	On some kind of emulated flash device and then hopefully the kernel will load the file system and then we can try and run stuff and see what breaks basically.
878590	880830	And then try and fix it and make it better.
882170	890190	For this one in particular, so KMU has this V express A9 board which has a cortex A9 core and flash memory.
890750	896690	It's no flash not NAND which is not a huge problem but in some cases it can be a little bit of a problem.
896690	900430	But just for the start, just for starting this is not a huge blocker.
900870	914830	There's a def config for the V express machine in Linux 2.6.36.4 and there's kind of machine code for the V express series like Mac V express code in the kernel as well.
914950	924630	So we can build the 2.6.36.4 kernel using the V express def config, using GCC 4.5.3 from build root 2012.
925710	932790	Getting that old version of GCC compiled with old build root can be a bit of a hassle.
932790	941230	So I'm not going to go into it here but it just involves using like old Ubuntu machines and kind of commenting out bits of code that don't work every now and again.
941230	944930	But it does get built and it does work.
947510	953850	Once the kernel is compiled and running, it kind of works pretty well.
953910	960230	The kernel loads into the V express machine.
961150	963410	You do need to edit the kernel config a little bit.
963410	970750	You have to make sure that you enable things like squash FS and MTD and the kind of stuff that you want to have in your rehosting kernel.
972590	976930	You also need to kind of think about things that you might want to compile into the kernel.
976930	979410	Because we're not going to be able to load anything later.
979410	981850	Like user space is not going to be able to load any kernel modules.
981850	986870	So we just want to, in this kernel we can kind of just build everything that we might potentially need into it.
986870	989570	As long as it's not incompatible with it with each other.
989910	994790	And it kind of makes life a little bit easier later on.
995850	999850	The first thing we have to do is figure out how to present the file system image to the kernel.
1000050	1002450	So we need to forge some kind of flash.
1002870	1007030	The flash needs to be laid out in a super predictable way.
1007870	1015410	But because we control the boot and the kernel, we can pass the MTD parts boot ARG to the kernel.
1015410	1020550	Which will tell it what our flash layout is and where to look for certain things.
1020550	1025110	So we can still use MTD block 2 as the place we're going to keep our file system.
1025150	1031770	And just pass it in the MTD parts boot ARG to tell the kernel that that's where it should boot from.
1032970	1034850	So the flash layout can look something like this.
1035290	1039050	We have a couple of imaginary empty parts, then the squash FS file system.
1039170	1047430	One of the quirks of VXpress boarding KME is it needs the file that's passed as flash to be exactly 64 megabytes.
1048130	1052770	But yeah, we can just put what we want in the place we want it and just pad the rest of the file out.
1054290	1064310	We compile the kernel, construct a KME command that looks a lot like the one up there and the file system doesn't unpack at all.
1064830	1067510	But we do get a relatively decent error.
1067550	1072430	The one is, it tells us it's failing because it's something to do with the compression of the squash FS file system.
1072530	1079990	So it's figured out it's squash FS but it can't do anything about it because it's figured out it uses some kind of unknown compression.
1080810	1088970	And the main problem is that the squash FS image that ships for the AC87U is compressed with EXED.
1089590	1099510	There's no native EXED compression support for squash FS in the main line kernel until 2.6.38 and we're on 2.6.36.4.
1099510	1103290	So Broadcom obviously does something a bit different.
1103870	1107350	And we can figure out what they're doing by looking at some of the GPL code.
1107810	1113890	The GPL code we got from the ASA site for this device has some squash FS related code that includes some EXED support.
1113890	1119490	So it's a slightly modified version, kind of back ported EXED support.
1120050	1128810	And similarly we can just back port EXED support from 2.6.38 into our own build or just use the code from this GPL release.
1129390	1130690	And it works.
1131690	1133570	The file system unpacks.
1134350	1143790	But it's a bit of an indication of the kinds of tweaks we have to make to the kernel as we're going along trying to do this kind of emulation effort.
1146870	1152710	The first thing we do is just pass BNSH as the init command.
1153690	1156350	And we can just drop into a shell and poke around a bit .
1156350	1161450	This is a bit like the situation we'd be in if we were doing the CH root kind of user space approach.
1161490	1165670	But of course nothing actually interesting runs because the system hasn't been initialized.
1165810	1168370	The file system is mounted but nothing has actually run.
1168850	1173810	We know that preinit is responsible for the initialization.
1173890	1176230	So let's have a little quick look at that.
1178030	1191050	So you can see here a lot of what the preinit code does is create directories, set up symlinks, copy files around into expected places, mounts pseudo file systems, normal init process stuff.
1192790	1199830	So yeah, in theory if we can get preinit running and completing, it will be a big step into being able to emulate the whole system.
1200150	1208610	There's another notable thing about preinit just from some static analysis, which is that it contains routines which only run when it's running with PID 1.
1208610	1211870	So we actually need it to be the init process in order for it to do stuff.
1211870	1218810	Like it's a kind of orchestrating process in the system, in this kind of holistic architecture of the system.
1219050	1222610	So we can't just invoke it from a shell and expect it to work.
1225410	1233550	We can just start the thing again with the init boot up pointing to preinit and see what fails basically.
1233550	1237810	I mean the big one is we got a whole load of NVRAM errors.
1238690	1240630	This is definitely expected.
1240630	1244890	NVRAM is the most common blocker in embedded Linux emulation.
1245170	1248630	So we need something to work to take it's place.
1252450	1256250	We kind of need to make a plan here.
1256490	1269570	We're not touching user space, we just need to change things in the kernel to create a device, a dev NVRAM basically, which would behave just like the dev NVRAM device would on the AC87U.
1269570	1279230	We can get a decent idea of what, how dev NVRAMs are actually interacted with by looking at the user space binaries in the firmware image itself.
1279230	1283830	So Brawl compromises these APIs to interact with NVRAM.
1283830	1292270	In particular this lib NVRAM SO, which is kind of the official Broadcom lib NVRAM.
1292270	1305370	On the 87U, user space binaries will import the lib NVRAM, the Broadcom lib NVRAM and call functions like NVRAM get, NVRAM set, NVRAM commit.
1306110	1315830	We can have a look at this, at this library and see what it's doing under the hood when it's doing these kinds of things and hopefully, yeah, this can inform what we're going to do.
1319210	1331490	So yeah, looking at lib NVRAM, it opens, it has a function called NVRAM in it, which opens dev NVRAM and stores the handle to it at this global memory location.
1331750	1340650	And then NVRAM get will call read on the open file handle with a pointer to a string of the key that wants to be read.
1340910	1343430	And another buffer where the value should be written.
1344770	1350730	NVRAM set calls write on the open file handle with a pointer to the key equals value string.
1350730	1356710	So it's using really basic sys calls that we can handle when we make our own kernel device that does something like this.
1357370	1359950	So yeah, we build a character device in the kernel.
1361530	1368550	In lib NVRAM, the whole NVRAM buffer is expected to be a contiguous block of key equals value string separated by null bytes.
1368550	1371010	So we can kind of just mirror what that is.
1371430	1373550	We do need somewhere to persist it.
1373550	1377690	So we can back it with just another partition in the flash memory.
1378450	1382090	We make sure that dev NVRAM, the file is created.
1382090	1383710	So user space can call open on it.
1383710	1388870	And then we just handle all the read, write, IOPTL operations as well.
1389030	1395390	And when we're doing that, we just look at what the Broadcom lib NVRAM is expecting to receive and we just return what it wants.
1395450	1398210	We're just kind of servants to the user space in that way.
1398910	1401850	And this is kind of a rough idea of the kind of code we have in the kernel.
1401850	1403610	I'm sorry you probably can't see it here .
1404350	1408750	But lib NVRAM is just doing super basic reads and writes.
1409230	1416970	We also handle calls to like NVRAM commit which is an IOPTL with a specific command number and we just write the buffer back to the flash when we get that.
1417810	1424110	And then there's calls like NVRAM get all which is just like NVRAM get but passes null to the second read arc.
1424230	1427590	And if we get that, we return all the NVRAM key value pairs.
1427950	1431050	So this is relatively, I have to say, quick to write.
1431690	1435150	We can write it, compile it, try it, squash bugs, compile it.
1435150	1437470	It's just kind of development life cycle stuff.
1437690	1440690	And the approach just kind of works.
1440690	1448410	And we can test it with the command line NVRAM tool that's in the ACES firmware and get all the data out of the sort of fake NVRAM partition.
1448850	1459030	But with this approach we just get a system wide NVRAM device that has persistent backing and works in the way that the user space is expecting.
1459150	1463450	And so it will probably work on other similar era devices that use the same mechanism as well.
1467870	1469850	Alongside doing this, we're also trying to emulate the thing.
1469850	1472470	So we have to solve the problem of what goes into the NVRAM.
1472730	1479010	And as I said earlier, the common wisdom is that we often need to be able to dump NVRAM from an actual device in order to do anything with it.
1479010	1483770	This isn't actually the case on the AC87U or any of the other ACES devices.
1483790	1487530	There's a lot of logic which populates NVRAM with default values.
1488170	1496330	And there's certain like NVRAM keys that you can set that will trigger a kind of full NVRAM reset.
1497010	1499750	So there's no need to have an actual physical device for these devices.
1499750	1503450	And we can kind of freestyle based off the defaults that pre-init creates.
1503990	1513120	And yeah, once we have NVRAM, pre-init kind of works a bit better and gets a bit further.
1513120	1516580	But we start hitting other issues that we weren't necessarily expecting.
1516580	1520300	So pre-init needs to be there to be more than one network interface.
1520300	1522880	It's looking for F1 but it can't find it.
1523120	1531380	We can't just add another interface without tweaking the V-Express machine and the kernel, Mac V-Express a little bit.
1531380	1533620	But we can quite easily do it.
1533920	1547460	We can just map a new ethernet interface into memory in the KMU code and in the Linux code we just add another like IRQ for the, in the Mac V-Express board files which lines up with the IRQ value in the KMU code.
1547980	1554260	It's a relatively light, light touch strategy but, yeah.
1558580	1564160	It's pretty, it's a little bit fiddly but it's not a huge amount to change but it, and it just kind of works.
1564160	1574000	So in our, in our, in our examples here I just kind of copied the V-Express machine, just created a new machine called BCM4709 with modifications.
1576580	1577860	And yeah.
1577860	1584920	So it just means we can add, like pass another net flag to KMU and we just get another, like a second network interface showing up in the guest.
1584920	1589580	We can do this as many times as we want but like two or three is probably the limit for what you need.
1590740	1592100	So pre-init's happy.
1592100	1593180	It's got two NICs.
1593180	1594400	We get different errors.
1594400	1597740	So we start to see more errors related to the JFFS2 directory.
1597840	1599280	This doesn't seem to exist.
1599400	1602300	There's no JFFS2 partition on the firmware image.
1602400	1603680	We can base it off.
1603680	1607120	So of the, of the firmware image downloaded from the ASUS site.
1607120	1610560	So assumedly there's something that's created or mounted at boot.
1610980	1615780	To figure out what it is, we just have to go back to pre-init and see what's going on.
1616620	1623800	Pre-init tries to mount certain partitions from the flash to the JFFS and ASUS JFFS directories.
1623960	1639160	It checks the proc MTD file to find partitions with names BRCM NAND and ASUS and then it just mounts those partitions as JFFS to those particular directories mentioned earlier.
1639860	1643260	And we can fix this just by modifying our flash again.
1643260	1645480	And just changing the boot ARGs again.
1646340	1648000	We modify the flash layout.
1648000	1652920	We make sure we tell the kernel which parts of the flash are called BRCM NAND and ASUS.
1652920	1662300	And that means when we boot the MTD file will contain those names that pre-init's expecting and pre-init will try to load those particular MTD parts as JFFS file system.
1662560	1663860	And this just works.
1663860	1665740	So the JFFS directories are created.
1665740	1666940	The files are getting written there.
1666940	1669720	Things like log files, sys log, certificates.
1669860	1673800	But it makes booting a lot smoother and a lot less stuff fails.
1676260	1680500	We're pretty far into the, into the init process but we get a lot of errors out on the terminal.
1680500	1684520	They're not like functionality blockers but they kind of pollute the terminal a lot .
1684860	1688460	In particular we get a lot of like dev GPIO related errors.
1688840	1693860	So to quieten down the machine we also create like a dummy GPIO interface.
1693860	1695500	A bit like the NVRAM one.
1697160	1703180	A lot like the NVRAM approach we look at user space binaries for GPIO interactions and see what's going on.
1703180	1715640	The binaries always just call, make an octal call on the open dev GPIO file with just specific operation codes and like a bit map of the pin that they're trying to read or write to.
1715640	1717920	That's being polled or poked basically.
1719340	1723820	With GPIO we need to know what the active low or the active high state is for each.
1723820	1727520	So luckily there's some device true files we can look at to reference that.
1727600	1731500	And then we can use that to determine which pins are expecting what.
1731860	1734780	And we just return a kind of neutral state back on them.
1735080	1739640	So it's a little bit trial and error but it's not a huge overhead.
1742890	1749430	And yeah, once we, once we get rid of the GPIO errors we get a few more kind of network interface issues.
1750010	1757570	We gave the system a couple of NICs but the driver for the NIC that KMU emulates doesn't actually behave how user space is emulating.
1758070	1765570	So the KMU machine is expecting the SMSC911X and the firmware is expecting some Broadcom device of some kind.
1765990	1776710	So the specific point in pre-init that's actually failing is a function called get file status which is checking the physical state of the interface by trying to read a register somewhere.
1777170	1788710	It does this again by making an IOPTL call to a socket handle but it just fails because the driver code for the SMSC911X doesn't know what to do with that particular code.
1789710	1793910	This isn't really a huge blocker because we don't need the solution to be kind of perfect or complete.
1793910	1798230	We're not trying to implement networking here but we just do a little bit of fraud in the kernel.
1798570	1806930	We modify the SMSC911X driver to intercept that particular operation code and then we just return whatever user space is expecting again.
1806990	1809910	Just to say, yeah it's physically up.
1810210	1814550	We just spam all the bits back like hi for this one in particular.
1816090	1818470	And yeah, at that point we're doing pretty well.
1818470	1829090	So we've got a system with a specific flash layout, fake MV RAM device, fake GPIO, slightly modified network driver and it mainly works.
1829090	1838310	The main thing, the final thing we kind of need to do to get it working, to be able to connect into it is to have a kind of LAN interface attachment.
1838310	1841190	And this is a solution we found for this.
1841650	1852830	So on the A7 you pre-init creates this like bridge device which takes the kind of LAN IP address which is 192.168.1.1.
1853670	1857690	F1 is one of the LAN interfaces for that.
1858190	1863970	So we can try to connect to whatever is on BR0 through F1.
1863970	1873690	And the cleanest way that we found to do this was to kind of create a tap on the host with a static IP in the same slash 24 range as the router and just make sure KMU knows how to use it .
1873690	1878070	So once you have that you can connect to the LAN interface of the device just like we would on the device itself.
1879490	1882730	And yeah, demo time.
1882730	1885370	Let me just try and get these demos, these demos .
1887170	1891430	Hold on.
1891430	1892090	Yeah.
1892550	1901830	So this is running the ASUS router here in the emulated one just on the right top right.
1902490	1906070	It takes a little minute to warm up.
1906250	1907930	So the video is a little bit long.
1908210	1909590	So apologies for that.
1910590	1921310	But you should see there I'm dropped into a shell on the kind of emulated, on the emulated AC87U there.
1921310	1924770	And I've just SSHed into it in the bottom tunnel there.
1924770	1927590	You can see all the processors are running in that.
1928970	1930370	A lot of them.
1930450	1933490	So assumedly the in it process kind of worked.
1933490	1936550	It got the system into a decent enough state to run stuff.
1936550	1940350	We've got a few network interfaces there as well.
1941210	1946730	And we've got a whole bunch of services running and a whole bunch of sockets open.
1947070	1952530	The interesting one there I guess is the web management interface.
1952530	1955330	A lot of the time this is the kind of high value one.
1955810	1962850	So I hope this is the right video and shows us the web management interface loading up.
1964490	1967190	It sometimes takes a little bit to get started.
1967190	1968350	But yeah, here we go.
1968570	1970550	So we've got the web management interface started.
1971670	1973390	We can log into it.
1973630	1975930	This one in particular had already been set up.
1976010	1981330	The set up process is a bit boring so I didn't kind of, I didn't keep that as a video.
1981330	1982390	But it's already been set up.
1982390	1987590	And the web management interface works almost entirely in this particular one.
1993760	1996340	And yeah, that's it.
2002790	2007410	So yeah, all the interesting services kind of work including the web management stuff.
2007410	2009870	You can configure SSH to run on boot.
2009870	2018750	You can change the NVRAM and you can do these things on the flash file itself or you can do it from inside the device itself, inside the emulator device itself.
2018990	2027110	In this particular one there's, in the AC87U there's a strange kind of parallel quantenna sock on there where it's expected to be on there which we didn't emulate.
2027110	2039010	But yeah, now we get a pretty decent like 95 percent functioning like emulation for this device with not a huge amount of layout on it.
2039030	2040850	And we can do it for other devices as well.
2040850	2046570	So other ASUS devices running the BCM4709 work basically immediately.
2046970	2049650	Netgear devices are quite quick to get running.
2049650	2058510	They're expecting another specific device to exist but as long as it exists in most cases it just kind of works.
2058650	2063450	Tender devices work as well but they use a different kind of NVRAM.
2064450	2076530	They handle all their own read writes and their NVRAM is called like CFM and it's just a partition in flash as well which just has like a CRC32 at the beginning.
2076530	2082630	But other than that it's just, it's essentially just key store, key value store.
2082790	2089810	Some devices are a bit more tricky, I guess partially because of the way the images are distributed.
2089810	2093130	So we might not have everything tied up with a nice bow.
2093470	2101230	Others expect like a really precise flash layout so it's more precision than just building a fake flash blob.
2101850	2106750	And there's always like a little ramp up period in like going from like blank NVRAM to a running system.
2106750	2111190	So there's a little bit of like the process of what breaks and why going on.
2111190	2115830	But in general the baseline principles are pretty much the same when you're doing all of them.
2116450	2123070	For a lot of these, getting devices from one particular vendor up and running might take a day.
2124750	2132050	And once you've got like one vendor kind of covered, it can maybe take an hour or so to get another device in that family up and running.
2132450	2136990	And I'll show you a few more if they work.
2138090	2165070	Yeah, this is a D-Link device, same configuration, same setup, also just the exact same emulation environment, just a different firmware, just a web management stuff is running in theory so we can have a look at that.
2165830	2169270	And yeah, there we go, there's the D-Link interface there.
2170550	2173850	You can poke around in that and have a lot of fun with that.
2175410	2177990	Another one now, this is the Netgear.
2178050	2185430	Again, with the Netgear one we can just, I think I sped up this one so we didn't have to wait around for the whole thing to warm up.
2185770	2196130	But once it's configured running, there's a few limitations like for the Netgear one, it's got this ping that it's trying to do before it will load the web interface.
2196130	2202930	But other than that, most of it works here as well, which is quite gratifying.
2203130	2212610	And then finally there's just one more example, there's a Chinese vendor called Ficom that I have never really heard of before.
2213010	2220130	But we've got one of those devices emulating as well, just because it's based on the same SOC.
2220150	2226130	So we have all of this potential based on this one emulation environment.
2235670	2240650	So yeah, conclusions, it's still early days proof of concept but it works.
2240650	2242510	It's relatively light touch.
2244370	2247910	Compiling support with these dummy peripherals is pretty smooth.
2247910	2251950	It's not totally fool proof and not everything is going to be working the whole time.
2252630	2255830	But just to get things running, it does seem to work.
2255830	2259570	We can tweak KMU machines when we need to, we can make changes when we need to.
2263110	2268190	We can take these same principles, we can move them to other SOCs as well.
2268530	2272070	We've started porting the same principles to other SOCs with some success.
2272530	2275690	Different SOCs have different profiles, the results may vary.
2277410	2288750	It can take a bit of effort in the first instance to create these environments but the return on investment can be quite high because you've got maybe tens or a hundred or so different devices you can emulate based off them.
2290630	2291910	Thanks for listening.
2291990	2295670	I'll stick around for a chat down here afterwards if anyone wants to have a chat.
2295670	2300850	Feel free to get in touch on my massively neglected Blue Sky account if you've got any questions.
2300930	2301570	Thanks.
