Lua.c32

From Syslinux Wiki
Revision as of 04:47, 12 August 2010 by Yoduh (talk | contribs) (Example: iterating and printing objects(tables))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


About

lua.c32 is a comboot module for Syslinux that consists of a Lua 5.1 interpreter and additional modules exposing syslinux and hardware internals.

Syntax

lua.c32 [script [arguments..]]

Modules

The following standard library modules are implemented:

  • coroutine
  • debug
  • io
  • package
  • string
  • table

The following standard library modules are not implemented:

  • math
  • os

The following syslinux library modules are available:

dmi module

  • gettable() - Returns a list if key-value pairs.
  • supported() - Returns true if DMI is supported on machine, false otherwise.

pci module

  • getidlist(filename) - Load a tab separated list of PCI IDs (pci.ids) and their description.
  • getinfo() - Return list of value pairs (device_index, device) of all PCI devices.

syslinux module

  • boot_it()
  • boot_linux(kernel, cmdline, [mem_limit], [videomode])
  • derivative() - Returns running Syslinux's derivative (ISOLINUX, PXELINUX or SYSLINUX).
  • filename(file) - Return name of file (loaded by loadfile())
  • filesize(file) - Return size of file (loaded by loadfile())
  • final_cleanup()
  • initramfs_add_file(initramfs, file) - Adds file to initramfs. initramfs needs to be initialized, file has been loaded by loadfile().
  • initramfs_init() - Return empty initramfs object
  • initramfs_load_archive(initramfs, filename) - Load contents of filename into initramfs. Initialize initramfs with initramfs_init() before use.
  • loadfile(filename) - Load file filename (via TFTP).
  • local_boot()
  • msleep(int) - Sleep fot int milliseconds.
  • run_command(cmd) - Execute syslinux command line command.
  • run_default()
  • run_kernel_image(kernel, cmdline, ipappend_flags, type)
  • sleep(int) - Sleep for int seconds.
  • version() - Returns syslinux version string.

vesa module

  • getmodes() - Return list of available VESA modes.
  • load_background(filename) - Load filename from TFTP, and use it as background image.
  • setmode() - Set (only currently supported) VESA mode.


Examples

Example: Scanning for available modules

Here is a simple example showing the available modules:

for k, v in pairs(package.loaded) do
  print(k, v)
end

Example: Loading lua.c32, running interpreter

Assuming you're booting via pxelinux and menu.c32 (other ways will work too), hit tab to get boot: prompt, erase your default boot cmd, and type "lua.c32<return>". This will load lua, and present a lua prompt. Then type the above example:

> lua.c32

Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> 
> for k, v in pairs(package.loaded) do
>> print(k,v)
>> end
string	table: 0x07fe3bc0
debug	table: 0x07fe4730
package	table: 0x07fe21f0
_G	table: 0x07fe03c0
pci	table: 0x07fe45a0
io	table: 0x07fe34e0
table	table: 0x07fe1600
dmi	table: 0x07fe4d30
vesa	table: 0x07fe5960
coroutine	table: 0x07fe1da0
syslinux	table: 0x07fe4fe0
> 

Example: printing tables

Expanding on above, you can print the table contents too:

> for k, t in pairs(package.loaded) do
>> for n,v in pairs (t) do
>> print(k, t, n, v)
>> end   
>> end
string	table: 0x07fe3bc0	sub	function: 0x07fe1b00
string	table: 0x07fe3bc0	upper	function: 0x07fe1b60
string	table: 0x07fe3bc0	len	function: 0x07fe4040
...
debug	table: 0x07fe4730	getupvalue	function: 0x07fe4a90
debug	table: 0x07fe4730	debug	function: 0x07fe48b0
debug	table: 0x07fe4730	sethook	function: 0x07fe4b20
...
package	table: 0x07fe21f0	preload	table: 0x07fe2760
package	table: 0x07fe21f0	loadlib	function: 0x07fe1e80
package	table: 0x07fe21f0	loaded	table: 0x07fe0e00
package	table: 0x07fe21f0	loaders	table: 0x07fe2350
package	table: 0x07fe21f0	cpath	./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so
package	table: 0x07fe21f0	config	/
;
?
!
-
package	table: 0x07fe21f0	path	./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua
package	table: 0x07fe21f0	seeall	function: 0x07fe22f0
_G	table: 0x07fe03c0	string	table: 0x07fe3bc0
_G	table: 0x07fe03c0	xpcall	function: 0x07fe0b60
_G	table: 0x07fe03c0	package	table: 0x07fe21f0
...

Example: Act on system.product_name DMI information

if (dmi.supported()) then

  dmitable = dmi.gettable()

  for k,v in pairs(dmitable) do
    print(k, v)
  end
	
  print(dmitable["system.manufacturer"])
  print(dmitable["system.product_name"])
  print(dmitable["bios.bios_revision"])

  if ( string.match(dmitable["system.product_name"], "ESPRIMO P7935") ) then
    print("Matches")
    syslinux.run_command("memdisk initrd=/dos/BIOS/FSC-P7935-108.img raw")
  else
    print("Does not match")
    syslinux.run_command("memdisk initrd=/dos/BIOS/default.img raw")
  end
end

Example: Print a list of PCI devices

-- get nice output
printf = function(s,...)
  return io.write(s:format(...))
end

-- get device info
pciinfo = pci.getinfo()

-- get plain text device description
pciids = pci.getidlist("/pci.ids")

-- list all pci buses
for dind,device in pairs(pciinfo) do

  -- search for device description
  search = string.format("%04x%04x", device['vendor'], device['product'])

  printf(" %04x:%04x:%04x:%04x = ", device['vendor'], device['product'],
      device['sub_vendor'], device['sub_product'])

  if ( pciids[search] ) then
    printf("%s\n", pciids[search])
  else
    printf("Unknown\n")
  end
end

-- print(pciids["8086"])
-- print(pciids["10543009"])
-- print(pciids["00700003"])
-- print(pciids["0070e817"])
-- print(pciids["1002437a1002437a"])

Example: Check syslinux derivative and behave differently

-- get nice output
printf = function(s,...)
  return io.write(s:format(...))
end

-- get syslinux derivative (ISOLINUX, PXELINUX, SYSLINUX)
derivative = syslinux.derivative()

printf("Run specific command depending on the Syslinux derivate:\n")
printf("--------------------------------------------------------\n\n")
printf("  Detected Syslinux derivative: %s\n", derivative)

if derivative == "SYSLINUX" then
  -- swap internal (hd1) hard drive with USB stick (hd0)
  commandline = 'chain.c32 hd1 swap'
elseif derivative == "ISOLINUX" then
  -- boot first hard drive
  commandline = 'chain.c32 hd0'
elseif derivative == "PXELINUX" then
  -- boot first hard drive
  commandline = 'chain.c32 hd0'
else
  printf("Do nothing\n")
  return 1
end

printf("\n  commandline for derivative:   %s\n\n", commandline)

-- Count down from 7
for time = 7, 1, -1 do
  printf("  Boot in %d second(s)...   \r", time)
  syslinux.sleep(1)
end

-- Boot
syslinux.run_command(commandline)

Example: Print kernel/initramfs information and load them

-- get nice output
printf = function(s,...)
  return io.write(s:format(...))
end
	
kernel = syslinux.loadfile("/SuSE-11.1/x86_64/linux")
printf("Filename/size: %s %d\n", syslinux.filename(kernel), syslinux.filesize(kernel))
	
initrd = syslinux.loadfile("/SuSE-11.1/x86_64/initrd")
printf("Filename/size: %s %d\n", syslinux.filename(initrd), syslinux.filesize(initrd))
	
initrd = syslinux.initramfs_init()
syslinux.initramfs_load_archive(initrd, "/SuSE-11.1/x86_64/initrd");
syslinux.boot_it(kernel, initrd, "init=/bin/bash")
	
syslinux.sleep(20)


Example: Print supported VESA modes

-- get nice output
printf = function(s,...)
  return io.write(s:format(...))
end
	
-- list available vesa modes
-- only one supported right now, not of much use
modes = vesa.getmodes()
	
for mind,mode in pairs(modes) do
   printf("%04x: %dx%dx%d\n", mode['mode'], mode['hres'], mode['vres'], mode['bpp'])
end

Further Reading

Lua documentation

Syslinux Lua documentation