從u-boot 移植 nds32 談 嵌入式系統開放原始碼開發的 一些經驗

48
從 u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗 Macpaul Lin OSDC.TW 2011

Upload: macpaul-lin

Post on 19-May-2015

3.451 views

Category:

Technology


9 download

TRANSCRIPT

Page 1: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

從 u-boot 移植 NDS32 談嵌入式系統開放原始碼開發的

一些經驗

MacpaulLinOSDC.TW2011

Page 2: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 2

這份投影片或許也對寫Kernel,Driver, 以及 C 語言

的人有幫助( 習慣良好的老鳥就不用聽這個了 )

Page 3: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 3

其實是在講怎樣射打 patch

Page 4: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 4

還有怎樣把爛 code 清乾淨

Page 5: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 5

因為本來要射打的 patch 都一直被打(槍)

Page 6: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 6

必要時還要幫老闆打槍打老闆的槍(PATCHv1startedfrom2010/08)

Page 7: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 7

一切為的都是豬頭 code 的愛與和平呀

Page 8: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 8

WhoAmI?

• MacpaulLin– Interestsandworks:

• OpenSource• Networking• OpenWrt(2006–2010)• U-boot(2010–2011)

– Contacts:• [email protected]([email protected])• http://www.plurk.com/macpaul• http://www.facebook.com/macpaul

Page 9: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 9

Outline

• Introductiontou-bootproject.• ThesoftwarearchitectureofU-boot.• Codecommitrulesandworkflow.• Suggestionsandexperiencessharing.

Page 10: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 10

U-boot-History

• http://en.wikipedia.org/wiki/Das_U-Boot• DasUnterseeboot

– Germanfor"thesubmarine."

• GPL• BootloaderforembeddedSystem

• PPC,ARM,AVR32,MIPS,x86,68k,Nios,andMicroBlaze.(andNDS32)

• U-BootoriginatedinworkdonebyMagnusDammona8xxPowerPCbootloadercalled8xxROM.

• WolfgangDenkmovedtheprojecttoSourceForge.net.

– http://www.denx.de/wiki/U-Boot/WebHome

Page 11: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 11

U-boot–BootLoader

• http://en.wikipedia.org/wiki/Boot_loader#Boot_loader

• Features– ItpreparsCPUandRAMtoaccessethe

nonvolatiledevicestoloadOSintoram.– Itprovidesfirmwareupgradeandfail-safe

functions.– Itcouldrunbasicdiagnosticandtesting.– Youcandevelopsimpleapplicationonu-boot.

• Italsosupportnon-OSframebufferdisplay(VFD).

Page 12: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 12

u-boot–Whyreleasecodetou-bootisimportant?

• Forcustomers,– Helponbootingtheirdevices.– MachinetypeIDisrelatedtoLinuxKernelbring

up.

• ReleaseandCleanupisimportant.– Bossusuallythinkcleanupdoesn'thelpon

contributiontothecompany.– Dirtycodeandoldcodeshouldbeconsideredas

garbageanddebt.• NoproductivitytoRD.

– ItmadeRDalwaysmaintainingoldpatcheswhennewreleaseonofficialprojectiscoming.

Page 13: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 13

U-boot–DevelopmentModel

• TaskForce– GateKeepers(custodian)bygitrepository.

• Functions,Architectures,andVendors.

• Schedule• DevelopmenthappensinReleaseCyclesof2months.• Thefirst2weeksarecalledMergeWindow,whichis

followedbyaStabilizationPeriod.• Patcheswithnewcodegetonlyacceptedwhilethe

MergeWindowisopen.• DuringtheStabilizationPeriod,onlypatchesthat

containbugfixesgetapplied.• Release.

Page 14: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 14

U-boot-Custodians

• TheCustodianstakeresponsibilityforsomearea(gitrepo)oftheU-Bootcode.

• http://www.denx.de/wiki/U-Boot/Custodians• http://patchwork.ozlabs.org/project/uboot/list/

Page 15: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 15

U-boot–Custodiangitrepo

• Themoststableandup-to-datemaintrunkwillbeunder"master"branch.

• Themostnewfeaturesandbugfixwillbein"next"branch.

Page 16: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 16

TheSystemviewofu-boot

(boards.cfg)

Target,    ARCH, CPU,      Board,    Vendor,   SoC

a320evb    arm   arm920t   ‐         faraday   a320

Sheevaplug arm   arm926ejs ‐         Marvell   kirkwood

adp‐ag101  nds32 n1213     adp‐ag101 AndesTech ag101

Page 17: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 17

Thesoftwarearchitectureofu-boot

u‐boot ‐ api       ‐ arch       ‐ board            ‐ common       ‐ drivers       ‐ examples       ‐ include       ‐ lib       ‐ net       ‐ tools

‐ nds32 – cpu ‐ soc        ‐ include        ‐ lib

Page 18: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 18

Bootprocessofu-boot!AndesbootStartupCode(resetvector)(start.S)

!

!1.bootstrap

!1.1reset-startofAndesboot

!1.2tosuperusermode-asiswhenreset

!1.4Dolowlevel_init

!-(thiswilljumpouttolowlevel_init.SinSoC)

!-(lowlevel_init)

!1.3Turnoffwatchdogtimer

!-(thiswilljumpouttowatchdog.SinSoC)

!-(turnoff_watchdog)

!2.Docriticalinitwhenreboot(notfrommem)

!3.Relocateandesboottoram

!4.Setupstack

!5.Jumptosecondstage(start_andesboot)

Page 19: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 19

Bootprocessofu-boot

arch/nds32/start.S ‐>arch/nds32/cpu/n1213/ag101/lowlevel_init.S ‐> arch/nds32/start.S ‐>arch/nds32/cpu/n1213/ag101/watchdog.S ‐> arch/nds32/start.S ‐> start_andesboot() (arch/nds32/lib/board.c)

Page 20: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 20

Bootprocessofu-boot

(arch/nds32/lib/board.c)

start_andesboot() ‐> init_sequence[] ‐>         arch_cpu_init()

        pmu_init()

        board_init()

        interrupt_init()

        timer_init()

        env_init()

        init_baudrate()

        serial_init() ...

Page 21: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 21

Bootprocessofu-boot

(board/AndesTech/adp‐ag101.c)

board_init()

dram_init()

board_eth_init()

board_flash_get_legacy()

Page 22: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 22

BoardandSystemconfiginu-boot

(include/configs/adp‐ag101.h)

#define CONFIG_ADP_AG101

#define CONFIG_SKIP_LOWLEVEL_INIT

#define CONFIG_SYS_TEXT_BASE   0x03200000

#define CONFIG_SYS_CLK_FREQ    48000000

#define CONFIG_BAUDRATE        38400

#define CONFIG_STACKSIZE       (128 * 1024)

#ifdef CONFIG_FTSDMC021

#include <faraday/ftsdmc021.h>

#define PHYS_SDRAM_0_SIZE       0x04000000 (64MB)

Page 23: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 23

Codecommitrulesandworkflow

• U-bootMailinglist:– [email protected]– http://lists.denx.de/mailman/listinfo/u-boot– Archives:http://lists.denx.de/pipermail/u-boot/

Page 24: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 24

u-boot–commitrules

• Patcheswithrelatedfeaturesgotogether.• Patchesmustbeorderedandbisected.

– HeaderscomesbeforeCfiles.– Conformedtomakeandbuildlogic.

• Patchesmustgowithcleardescriptions.– Subject(incategory)– Descriptions– Signed-off

• Revisedpatchesmustbelabeledwithversionsandsimplechangelog.

• Custodianswilltestandapplythepatches.

Page 25: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 25

Patcheswithrelatedfeaturesgotogether

• Usegitrebase-iHEAD~7– gitcommit--amend– gitrebase--continue

nds32/ag101/adp-ag101:addboardadp-ag101support

nds32:commonbdinfo,bootm,imagesupport

nds32:standalonesupport

nds32/lib:addgenericfuncsinNDS32lib

nds32/ag101:cpuandinitfuncsofSoCag101

nds32/coreN1213:NDS32N12corefamilyN1213

nds32:addheaderfilessupportfornds32

Page 26: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 26

Splitgitcommits

• 我接手 u-boot 的時候整包 code 全部被綁在2 個 patch(gitcommits) 裡面

• patchutils(doesn'tcompatiblewithgit)– filterdiff

• Samesinglepatchfileintohunks.

– splitdiff• Splitouteverysinglefile-levelpatch.

Page 27: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 27

Patchesmustgowithcleardescriptions.

Author:     Macpaul Lin <[email protected]>

AuthorDate: Mon Oct 25 14:03:51 2010 +0800

Commit:     macpaul <[email protected]>

CommitDate: Sat Mar 26 00:54:17 2011 +0800

    nds32: add NDS32 support into common header file

    Add NDS32 support into common header file.

    Signed‐off‐by: Macpaul Lin <[email protected]>

subject

description

signed-offgit commit -s

do not use:bug number

Page 28: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 28

Revisedpatchesmustbelabeledwithversions

andsimplechangelogSigned‐off‐by: Macpaul Lin <[email protected]>

‐‐‐

Changes for v1‐v4:

   ‐ Code cleanup and style formatting.

Changes for v5‐v6:

   ‐ This patch also updated the following changes against the

     change after master tree (v2010.12‐rc1).

   ‐ fix upper case definitions in cache.h

   ‐ Support GD_FLG_ENV_READY and env_buf vars in nds32 global_data.h.

   ‐ Add readsb, writesb functions into io.h.

http://www.kernel.org/pub/software/scm/git/docs/user‐manual.html#cleaning‐up‐history

Page 29: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 29

Usegitsend-emailtosendthepatch

• Rememberedaddthemailinglistandrelateddevelopersintothecc.list.

• gitsend-emailgoesplaintext.– Donotaddpatchasanattachment.– Donotusemime-typeinmailheader.– Donotallowyourcompanyadd"copyright"and

"confidential"inthelastsectionofyourpatch.

Page 30: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 30

Codingstyle

• LinuxCodingstyle.– http://lxr.linux.no/#linux+v2.6.38/Documentati

on/CodingStyle– linux/scripts/checkpatch.pl

• U-bootCodingstyle.– http://www.denx.de/wiki/U-Boot/CodingStyle– http://lxr.linux.no/linux/Documentation/CodingS

tyle

Page 31: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 31

U-boot-Codingstyle

• Allvariables,functions,marcosgoestofulluppercaseorlowercase.– Donotuse“intIE6mustDie;"declaration.

• Youmustusemodelnametodeclareyourdevicedefines.

• Deviceoffsetanddefines 在 headerfile 宣告必須使用產品 " 型號 " ,

• Ex:useFTSDMC021,FTSDMC020,– Donotusefunctiondescription:“DRAM_CONTROLLER"

• Ex:CONFIG_FTSDMC021_BANK_SIZE_1M– Donotuse:SDRAM_BANK_SIZE_1M

Page 32: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 32

U-boot-Codingstyle

• FilesinMakefileorinconfig.mkmustbeordered.– Afilebeginswith'a'mustgoesbeforethenother

beginswith'b'.– Ex:

+START=start.o+COBJS=interrupts.ocpu.o<--- 這樣會被打槍

– Pleasekeepallsuchlistssorted.'c'<'i'.

Page 33: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 33

U-boot-Codingstyle

• Globalcodemustneverincludeanyboardspecificheaderfiles.– Thisisfundeamentallyflawed.– WrongEx:#include <board/AndesTech/include/porting.h>

#include "../../../board/AndesTech/include/porting.h"

• DeclareregisteroffsetsbyCstructures,donotuse#define.

• Ifdefinitionsofregisteroffsetsusedbyassemblycodeisnecessary,usetool(make-asm-offset)togeneratetheasm.code.

Page 34: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 34

U-boot-Codingstyle

• Comment:– Ex:/*

 * reset handler

 */

or 

/* status register */

– WrongEx:/****************************

 * reset handler

 *===========================

 */

// macpaul: bug fix: 2923: halt

Page 35: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 35

Codingstyle

• Codingstyleisrelatedtoversioncontrolsystem.– git(diff)isamassofthestackofpatches.– Reducedependancebetweenpatches.– Reduceunnecessaryanduselessobject,log,

historyinversioncontrolrepository.

• Donotfollowtheolddays'codingstyle.– Pleasekeepyourcodingstylerulesupdated.– Especiallydonotcopyfrom“ARM”and

“x86”code.

Page 36: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 36

Codingstyle

• NoChineseincomment.if(sound)/* 開聲音 */

writeb(0x1,dev->mute);

• Donotexceed80charactersinasingleline.– ~/.vimrc

highlightOverLengthctermbg=redctermfg=whiteguibg=#592929matchOverLength/\%81v.\+/

• Donotuse#if0and#if1.– Usemeaningful#defineCONFIG_OSDC_TW_R1

Page 37: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 37

Codingstyle-misc

– Donotuse//...ascomment.• pleaseuse/*…*/

– Becarefulonpointstatement• Ex:void*addr• WrongEx:

– void*addr

– Becarefulonloopstatement• Ex:

– for(i=0;i<=5;i++)

– Becarefulonbracestatementwithcorrectspace.

• Ex:voidtest(voidtarget,voidcase)

Page 38: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 38

Codingstyle

• Youmustmaintainthecommityearofyourcopyright/copyleftstatement.– 不論 code 多老,只要 commit 沒有進

maintrunk ,都必須使用「真正」進 commit 的年份。

– Ex:#(C)Copyright2006MacpaulLin,Andestech<[email protected]>

• Wolfgang'scomment:– Sonoworkhasbeendoneonthisinthelast4years?I

guessyouwanttoupdateyourCopyrightmessages-allofthem.

Page 39: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 39

Codingstyle

• Thebeginningofafunctionsisdeclaration.– Donotputcommentsatfirst.– WrongEx:

voidcleanup_before_linux(void){

/*thisfunctioniscalledjustbeforewecalllinux*/

inti;}

Page 40: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 40

Codingstyle

• Functionaldescriptiongoesbeforethenfunction.– Ex:

/* * reset handler */void do_reset(){

Page 41: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 41

Codingstyle

• 每一行行尾不能有空白或者 \t 字元結尾,所有的行尾都必須是非空白字元結尾。– Notrailingspaces.

• "\t\t$","\t$","\t"

– Nospacesatthebeginning.– Donotusemixed"\t"todoindent.

Page 42: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 42

Codingstyle–volatile

• Whatisvolatile?– http://en.wikipedia.org/wiki/Volatile_variable– http://en.wikipedia.org/wiki/Volatile_variable#Op

timization_comparison_in_C

• DonotusevolatileinLinux,driverandbootloader.– http://lxr.linux.no/#linux+v2.6.38/Documentati

on/volatile-considered-harmful.txt

Page 43: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 43

Codingstyle–volatile

• Likevolatile,thekernelprimitiveswhichmakeconcurrentaccesstodatasafe(spinlocks,mutexes,memorybarriers,etc.)aredesignedtopreventunwantedoptimization.– Properspinlock.

• Volatilevariableswithoutspinlockstillcauseproblem.

– Memory-mappedI/Oregistermustbeaccessedbyaccessingfunctionswithlock.

– PropercodeforbusyWaiting.while(my_variable!=what_i_want)

cpu_relax();

Page 44: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 44

Codingstyle–volatile

• Therearestillafewraresituationswherevolatilemakessenseinthekernel.

• AccessorfunctionsmightusevolatileonwheredirectI/Omemoryaccessdoeswork.

• Inlineassemblycodewhichchangesmemory,butwhichhasnoothervisiblesideeffects,risksbeingdeletedbyGCC.

• Thejiffiesvariableisspecialinthatitcanhaveadifferentvalueeverytimeitisreferenced,butitcanbereadwithoutanyspeciallocking.

• PointerstodatastructuresincoherentmemorywhichmightbemodifiedbyI/Odevicescan,sometimes,legitimatelybevolatile.

Page 45: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 45

Codingstyle–volatile

• Wolfgang:– Note:thisalsoandespeciallyappliestothe

"volatile"and"usingI/Oaccessorsinsteadofpointeracesses"issues.

– [Ifnotyet,youwouldprobablysoonrunintoissueswhenswitchingtoGCC4.5.xorevenGCC4.6.]

• https://nongnu.org/software/gcc/gcc-4.6/changes.html

• Anewswitch-fstrict-volatile-bitfieldshasbeenadded.Usingitindicatesthataccessestovolatilebitfieldsshoulduseasingleaccessofthewidthofthefield'stype.

Page 46: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 46

CodingStyle–atesting

printf("\n");

printf("pc : [<%08lx>]  sp: [<%08lx>]\n"

       "ra : %08lx  gp : %08lx  fp : %08lx\n",

        regs‐>PC, regs‐>SP, regs‐>RA, regs‐>GP, regs‐>FP);

printf("D1H: %08lx  D1L: %081x  D0H: %08lx  D0L: %08lx\n",

        regs‐>D1HI, regs‐>D1LO, regs‐>D0HI, regs‐>D0LO);

printf("r27: %08lx  r26: %08lx  r25: %081x  r24: %08lx\n",

        regs‐>R27, regs‐>R26, regs‐>R25, regs‐>R24);

printf("r23: %08lx  r22: %081x  r21: %08lx  r20: %08lx\n",

        regs‐>R23, regs‐>R22, regs‐>R21, regs‐>R20);

Page 47: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 47

Release early, release often.

Page 48: 從u-boot 移植 NDS32 談 嵌入式系統開放原始碼開發的 一些經驗

MacpaulLin-OSDC.TW20112011/03/25 48

Q & A.(Patch v7 for NDS32 porting is under cleanup.)